




免費預覽已結束,剩余7頁可下載查看
下載本文檔
版權說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權,請進行舉報或認領
文檔簡介
實驗 JDBC基礎(3)一、相關知識點1、JDBC基本概念2、JDBC數(shù)據(jù)增、刪、改,事務控制等二、實驗目的:理解Java連接數(shù)據(jù)庫的基本概念。理解利用Statement對象、PreparedStatement對象進行增、刪、改操作,理解事務的概念和JDBC編程方式。三、實驗內(nèi)容:1、 利用Statement對象進行數(shù)據(jù)添加。第一步:修改PublisherManager類的createPublisher方法,將其中的insert語言改成用Statement對象執(zhí)行;第二步:運行圖書管理系統(tǒng),進行添加出版社測試?!緦嶒灲Y果與分析】A、 寫出替換的代碼部分。Connection conn=null;try conn=DBUtil.getConnection();String sql=select * from BeanPublisher where pubid=+p.getPubid()+;java.sql.Statement st=conn.createStatement();/st.setString(1,p.getPubid();java.sql.ResultSet rs=st.executeQuery(sql);if(rs.next() throw new BusinessException(出版社編號已經(jīng)被占用);rs.close();st.close();sql=select * from BeanPublisher where publisherName=+p.getPublisherName()+;st=conn.createStatement();/st.setString(1, p.getPublisherName();rs=st.executeQuery(sql);if(rs.next() throw new BusinessException(出版社名稱已經(jīng)存在);rs.close();st.close();sql=insert into BeanPublisher(pubid,publisherName,address) values(+p.getPubid()+,+p.getPublisherName()+,+p.getAddress()+);st=conn.createStatement();/st.setString(1, p.getPubid();/st.setString(2, p.getPublisherName();/st.setString(3,p.getAddress();st.execute(sql);st.close(); catch (SQLException e) e.printStackTrace();throw new DbException(e);2、 利用insert語句添加數(shù)據(jù)時,未指定字段值處理。第一步:運行圖書管理系統(tǒng),打開讀者類別管理界面,并嘗試添加一個讀者類別;系統(tǒng)將會報一個錯誤,請分析說明錯誤原因。reader.Typeid 是主碼 ,不能為空 第二步:通過數(shù)據(jù)庫表結構的修改,解決上述問題。并用同樣的方式解決圖書借閱功能的bug。打開 企業(yè)管理器 ;打開 beanreadertype 表;打開 設計表;將 標識改成 是 ; 第三步:如果表結構不修改,應該如何修改程序,使新增讀者類別的ID為表中現(xiàn)有數(shù)據(jù)的最大ID值+1。public void createReaderType(BeanReaderType rt) throws BaseExceptionif(rt.getReaderTypeName()=null | .equals(rt.getReaderTypeName() | rt.getReaderTypeName().length()20)throw new BusinessException(讀者類別名稱必須是1-20個字);if(rt.getLendBookLimitted()100)throw new BusinessException(借閱圖書數(shù)量必須在0-100之間);Connection conn=null;try conn=DBUtil.getConnection();String sql=select * from BeanReaderType where readerTypeName=?;java.sql.PreparedStatement pst=conn.prepareStatement(sql);pst.setString(1, rt.getReaderTypeName();java.sql.ResultSet rs=pst.executeQuery();if(rs.next() throw new BusinessException(讀者類別名稱已經(jīng)被占用);rs.close();pst.close(); sql=select max(readerTypeId)from BeanReadertype;int i=1;pst=conn.prepareStatement(sql);rs = pst.executeQuery();/id=rs.getint +1;if(!rs.next()/是否已經(jīng)有idi=1;elsei+=rs.getInt(1);sql=insert into BeanReaderType(readerTypeId,readerTypeName,lendBookLimitted) values(?,?,?);pst=conn.prepareStatement(sql);pst.setInt(1,i);pst.setString(2, rt.getReaderTypeName();pst.setInt(3,rt.getLendBookLimitted();pst.execute();rs.close();pst.close(); catch (SQLException e) e.printStackTrace();throw new DbException(e);finallyif(conn!=null)try conn.close(); catch (SQLException e) / TODO Auto-generated catch blocke.printStackTrace();3、 利用PreparedStatement對象進行數(shù)據(jù)修改。在SystemUserManager類中,新建一個modifyUserName方法,實現(xiàn)用戶名稱(username字段)的修改功能。并修改其main函數(shù),將admin用戶的名稱改為:超級管理員?!緦嶒灲Y果與分析】A、 請?zhí)峁┓椒ùa和main函數(shù)代碼。public void modifyUserName(String username)throws BaseExceptionConnection conn=null;try conn=DBUtil.getConnection();String sql = update beansystemuser set username = +username+ where userid=admin;java.sql.Statement st=conn.createStatement();int rs = st.executeUpdate(sql); catch (SQLException e) e.printStackTrace();throw new DbException(e);finallyif(conn!=null)try conn.close(); catch (SQLException e) / TODO Auto-generated catch blocke.printStackTrace();public static void main(String args)BeanSystemUser user=new BeanSystemUser();user.setUserid(admin);user.setUsername(系統(tǒng)管理員);user.setUsertype(管理員);try new SystemUserManager().modifyUserName(超級管理員); catch (BaseException e) / TODO Auto-generated catch blocke.printStackTrace();B、 思考:如果上述方法的返回值為布爾類型,即如果成功修改了用戶名稱,則返回true,如果用戶不存在或修改失敗返回false。應該如何完善代碼。提示:主要statement或PreparedStatement對象的execute方法和executeUpdate方法的區(qū)別。public static void main(String args)BeanSystemUser user=new BeanSystemUser();user.setUserid(admin);user.setUsername(系統(tǒng)管理員);user.setUsertype(管理員);try new SystemUserManager().modifyUserName(超級管理員1); catch (BaseException e) / TODO Auto-generated catch blocke.printStackTrace();public void modifyUserName(String username)throws BaseExceptionConnection conn=null;try conn=DBUtil.getConnection();String sql = update beansystemuser set username = +username+ where userid=admin;java.sql.Statement st=conn.createStatement(); boolean rs = st.execute(sql); if(rs = true) System.out.print(ok); else System.out.print(no); catch (SQLException e) e.printStackTrace();throw new DbException(e);finallyif(conn!=null)try conn.close(); catch (SQLException e) / TODO Auto-generated catch blocke.printStackTrace();4、 Delete語句的執(zhí)行。修改用戶管理類中的用戶刪除方法,用刪除數(shù)據(jù)庫表中數(shù)據(jù)的形式代替現(xiàn)有軟刪除模式?!緦嶒灲Y果與分析】A、修改后的sql語句部分是。sql=delete BeanSystemUser where userid=?; /delete BeanSystemUser where userID=001B、如果對刪除函數(shù)進行限制,要求不能刪除已經(jīng)有過借閱操作的用戶。應如何修改代碼。提示:可參考讀者管理類中的讀者類別刪除方法。public void deleteUser(String userid)throws BaseExceptionConnection conn=null;try conn=DBUtil.getConnection();String sql=select removeDate from BeanSystemUser where userid=?;java.sql.PreparedStatement pst=conn.prepareStatement(sql);pst.setString(1,userid);java.sql.ResultSet rs=pst.executeQuery();if(!rs.next() throw new BusinessException(登陸賬號不 存在);if(rs.getDate(1)!=null) throw new BusinessException(該賬號已經(jīng)被刪除);rs.close();pst.close();sql=select * from BeanBookLendRecord where lendOperUserid=?;pst=conn.prepareStatement(sql);pst.setString(1,userid);rs=pst.executeQuery();if(!rs.next()sql=delete BeanSystemUser where userID=?;pst=conn.prepareStatement(sql);pst.setString(1, userid);pst.execute();pst.close();elsethrow new BusinessException(該賬號已有借閱信息,不能刪除); catch (SQLException e) e.printStackTrace();throw new DbException(e);finallyif(conn!=null)try conn.close(); catch (SQLException e) / TODO Auto-generated catch blocke.printStackTrace();5、 在數(shù)據(jù)庫中建立一張BeanBookLendRecord_backup表,用于保存已經(jīng)歸還圖書的借閱記錄。其表結構與BeanBookLendRecord表完全一致。要求在借閱管理類中,增加方法,實現(xiàn)已經(jīng)歸還數(shù)據(jù)的備份功能(備份完成后,在原表中刪除備份成功的數(shù)據(jù))。提示:注意事務控制?!緦嶒灲Y果與分析】A 請?zhí)峁﹤浞荼淼慕ū碚Z句select * into BeanBookLendRecord_backup from BeanBookLendRecordB 請?zhí)峁﹤浞莺瘮?shù)代碼public void a() java.sql.Connection con = null;try con = DBUtil.getConnection();String sql = insert into BeanBookLendRecord_backup select * from BeanBookLendRecord;java.sql.Statement st = con.createStatement(); java.sql.ResultSet rs = st.executeQuery(sql); sql=delete from BeanBookLendRecord; st=con.createStatement(); rs=st.executeQuery(sql);catch(SQLException e)e.printStackTrace();finallyif( con != null)try con.close(); catch (SQLException e)e.printStackTrace(); 6、 如果需要記錄圖書的入庫時間(需要包含時分秒),應如何修改數(shù)據(jù)庫表結構和相關代碼?【實驗結果與分析】在bookbeak中添加:localdate public void createBook(BeanBook b) throws BaseExceptionif(b.getBarcode()=null | .equals(b.getBarcode() | b.getBarcode().length()20)throw new BusinessException(條碼必須是1-20個字);if(b.getBookname()=null | .equals(b.getBookname() | b.getBookname().length()50)throw new BusinessException(圖書名稱必須是1-50個字);Connection conn=null;try conn=DBUtil.getConnection();String sql=select * from Bean
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 湖北藝術職業(yè)學院《機械制造工藝學課程設計》2023-2024學年第二學期期末試卷
- 濟南護理職業(yè)學院《人力資源管理(Ⅲ)》2023-2024學年第二學期期末試卷
- 閩江學院《廣告學》2023-2024學年第二學期期末試卷
- 洛陽科技職業(yè)學院《媒體與社會》2023-2024學年第二學期期末試卷
- 仰恩大學《住宅建筑設計原理》2023-2024學年第二學期期末試卷
- 陜西師范大學《路橋工程施工與養(yǎng)護管理》2023-2024學年第二學期期末試卷
- 貴州財經(jīng)大學《信號與系統(tǒng)A(雙語)》2023-2024學年第二學期期末試卷
- 青島工程職業(yè)學院《中級公司金融》2023-2024學年第二學期期末試卷
- 生命教育:悅納他人是一種智慧
- 2024年液壓破碎錘項目資金申請報告代可行性研究報告
- 合同到期協(xié)議書(3篇)
- 2022年教學教材《石油裂解與乙烯》精品優(yōu)秀教案
- IPC-A-610國際標準中英文對照(doc 17)
- 山大《毛澤東思想和中國特色社會主義理論體系概論》教案第3章 社會主義改造理論
- 部編版四年級下冊語文全一冊期末總復習—重點歸納整理
- (國開)2019年春電大本科水利水電工程造價管理形考3答案
- 金普新區(qū)預防性體檢人員審核表
- 礦山地質(zhì)環(huán)境保護與治理恢復方案編制規(guī)范2011
- 重慶市新建居民住宅小區(qū)供配電設施建設管理辦法
- 【精品畢業(yè)論文】Bi2WO6光催化劑的熔鹽法合成
- 正確解讀檢驗報告單 ppt課件
評論
0/150
提交評論