數(shù)據(jù)庫系統(tǒng)應用與開發(fā)實驗四_第1頁
數(shù)據(jù)庫系統(tǒng)應用與開發(fā)實驗四_第2頁
數(shù)據(jù)庫系統(tǒng)應用與開發(fā)實驗四_第3頁
數(shù)據(jù)庫系統(tǒng)應用與開發(fā)實驗四_第4頁
數(shù)據(jù)庫系統(tǒng)應用與開發(fā)實驗四_第5頁
已閱讀5頁,還剩4頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

1、實驗JDBC基礎(3)一、有關(guān)知識點1、JDBC基本觀點2、JDBC數(shù)據(jù)增、刪、改,事務控制等二、實驗目的:理解Java連結(jié)數(shù)據(jù)庫的基本觀點。理解利用Statement對象、PreparedStatement對象進行增、刪、改操作,理解事務的觀點和JDBC編程方式。三、實驗內(nèi)容:1、利用Statement對象進行數(shù)據(jù)增添。第一步:改正PublisherManager類的createPublisher方法,將此中的insert語言改成用Statement對象執(zhí)行;第二步:運轉(zhuǎn)圖書管理系統(tǒng),進行增添第一版社測試?!緦嶒灲Y(jié)果與剖析】A、寫出替代的代碼部分。Connectionconn=null;tr

2、yconn=DBUtil.getConnection();Stringsql=select*fromBeanPublisherwherepubid=conn.createStatement();/st.setString(1,p.getPubid();if(rs.next()thrownewBusinessException(rs.close();st.close();sql=select*fromBeanPublisherwherepublisherName=+p.getPublisherName()+;+p.getPubid()+第一版社編號已經(jīng)被占用);st=conn.createSta

3、tement();st.setString(1,p.getPublisherName();rs=st.executeQuery(sql);if(rs.next()thrownewBusinessException(rs.close();st.close();第一版社名稱已經(jīng)存在);sql=insertintoBeanPublisher(pubid,publisherName,address)values(+p.getPubid()+,+p.getPublisherName()+,st=conn.createStatement();/st.setString(1,p.getPubid();/st

4、.setString(2,p.getPublisherName();/st.setString(3,p.getAddress();st.execute(sql);st.close();catch(SQLExceptione)e.printStackTrace();thrownewDbException(e);+p.getAddress()+);2、利用insert語句增添數(shù)據(jù)時,未指定字段值辦理。第一步:運轉(zhuǎn)圖書管理系統(tǒng),翻開讀者類型管理界面,并試試增添一個讀者類型;系統(tǒng)將會報一個錯誤,請剖析說明錯誤原由。reader.Typeid是主碼,不可以為空第二步:經(jīng)過數(shù)據(jù)庫表構(gòu)造的改正,解決上述問題

5、。并用相同的方式解決圖書借閱功能的bug。翻開公司管理器;翻開beanreadertype表;翻開設計表;將表記改成是;第三步:假如表構(gòu)造不改正,應當怎樣改正程序,使新增讀者類其他ID為表中現(xiàn)有數(shù)據(jù)的最大ID值+1。publicvoidcreateReaderType(BeanReaderTypert)if(rt.getReaderTypeName()=nullrt.getReaderTypeName().length()20)thrownewBusinessException(throwsBaseException|.equals(rt.getReaderTypeName()|讀者類又名稱一

6、定是1-20個字);if(rt.getLendBookLimitted()100)thrownewBusinessException(借閱圖書數(shù)目一定在0-100之間);Connectionconn=null;tryconn=DBUtil.getConnection();Stringsql=select*fromBeanReaderTypewherereaderTypeName=?pst.setString(1,rt.getReaderTypeName();if(rs.next()thrownewBusinessException(讀者類又名稱已經(jīng)被占用rs.close();pst.close

7、();sql=selectmax(readerTypeId)fromBeanReadertype;inti=1;pst=conn.prepareStatement(sql);rs=pst.executeQuery();/id=rs.getint+1;if(!rs.next()/能否已經(jīng)有idi=1;);elsei+=rs.getInt(1);sql=insertintoBeanReaderType(readerTypeId,readerTypeName,lendBookLimitted)values(?,?,?)pst=conn.prepareStatement(sql);pst.setInt

8、(1,i);pst.setString(2,rt.getReaderTypeName();pst.setInt(3,rt.getLendBookLimitted();pst.execute();rs.close();pst.close();catch(SQLExceptione);e.printStackTrace();thrownewDbException(e);finallyif(conn!=null)tryconn.close();catch(SQLExceptione)/TODOAuto-generatedcatchblocke.printStackTrace();3、利用Prepar

9、edStatement對象進行數(shù)據(jù)改正。在SystemUserManager類中,新建一個modifyUserName方法,實現(xiàn)用戶名稱(功能。并改正其main函數(shù),將admin用戶的名稱改為:超級管理員。username字段)的改正【實驗結(jié)果與剖析】A、請供給方法代碼和main函數(shù)代碼。publicvoidmodifyUserName(Stringusername)throwsBaseExceptionConnectionconn=null;tryconn=DBUtil.getConnection();Stringsql=updatebeansystemusersetusername=use

10、rid=admin;intrs=st.executeUpdate(sql);catch(SQLExceptione)e.printStackTrace();thrownewDbException(e);+username+wherefinallytryconn.close();catch(SQLExceptione)/TODOAuto-generatedcatchblocke.printStackTrace();publicstaticvoidmain(Stringargs)BeanSystemUseruser=newBeanSystemUser();user.setUserid(admin)

11、;user.setUsername(系統(tǒng)管理員);user.setUsertype(管理員);trynewSystemUserManager().modifyUserName(catch(BaseExceptione)/TODOAuto-generatedcatchblock超級管理員);e.printStackTrace();B、思慮:假如上述方法的返回值為布爾種類,即假如成功改正了用戶名稱,則返回true,假如用戶不存在或改正失敗返回false。應當怎樣完美代碼。提示:主要statement或PreparedStatement對象的execute方法和executeUpdate方法的差別。

12、publicstaticvoidmain(Stringargs)BeanSystemUseruser=newBeanSystemUser();user.setUserid(admin);user.setUsername(系統(tǒng)管理員);user.setUsertype(管理員);trynewSystemUserManager().modifyUserName(超級管理員1);catch(BaseExceptione)TODOAuto-generatedcatchblocke.printStackTrace();publicvoidmodifyUserName(Stringusername)thr

13、owsBaseExceptionConnectionconn=null;tryconn=DBUtil.getConnection();Stringsql=updatebeansystemusersetusername=+username+whereuserid=admin;booleanrs=st.execute(sql);if(rs=true)System.out.print(ok);elseSystem.out.print(no);catch(SQLExceptione)e.printStackTrace();thrownewDbException(e);finallyif(conn!=n

14、ull)tryconn.close();catch(SQLExceptione)TODOAuto-generatedcatchblocke.printStackTrace();4、Delete語句的履行。改正用戶管理類中的用戶刪除方法,用刪除數(shù)據(jù)庫表中數(shù)據(jù)的形式取代現(xiàn)有軟刪除模式?!緦嶒灲Y(jié)果與剖析】A、改正后的sql語句部分是。sql=deleteBeanSystemUserwhereuserid=?;/deleteBeanSystemUserwhereuserID=001B、假如對刪除函數(shù)進行限制,要求不可以刪除已經(jīng)有過借閱操作的用戶。應怎樣改正代碼。提示:可參照讀者管理類中的讀者類型刪除方

15、法。publicvoiddeleteUser(Stringuserid)throwsBaseExceptionConnectionconn=null;tryconn=DBUtil.getConnection();Stringsql=selectremoveDatefromBeanSystemUserwhereuserid=?;pst.setString(1,userid);if(!rs.next()thrownewBusinessException(登岸賬號不存在);if(rs.getDate(1)!=null)thrownewBusinessException(該賬號已經(jīng)被刪除);rs.cl

16、ose();pst.close();sql=select*fromBeanBookLendRecordwherelendOperUserid=?;pst=conn.prepareStatement(sql);pst.setString(1,userid);rs=pst.executeQuery();if(!rs.next()sql=deleteBeanSystemUserwhereuserID=?;pst=conn.prepareStatement(sql);pst.setString(1,userid);pst.execute();pst.close();elsethrownewBusine

17、ssException(該賬號已有借閱信息,不可以刪除);catch(SQLExceptione)e.printStackTrace();thrownewDbException(e);finallyif(conn!=null)tryconn.close();catch(SQLExceptione)TODOAuto-generatedcatchblocke.printStackTrace();5、在數(shù)據(jù)庫中成立一張BeanBookLendRecord_backup表,用于保留已經(jīng)送還圖書的借閱記錄。其表構(gòu)造與BeanBookLendRecord表完整一致。要求在借閱管理類中,增添方法,實現(xiàn)已經(jīng)送

18、還數(shù)據(jù)的備份功能(備份達成后,在原表中刪除備份成功的數(shù)據(jù))。提示:注意事務控制?!緦嶒灲Y(jié)果與剖析】請供給備份表的建表語句select*intoBeanBookLendRecord_backupfromBeanBookLendRecord請供給備份函數(shù)代碼publicvoida()null;trycon=DBUtil.getConnection();Stringsql=insertintoBeanBookLendRecord_backupselect*fromBeanBookLendRecord;createStatement();rs=st.executeQuery(sql);sql=dele

19、tefromBeanBookLendRecord;st=con.createStatement();rs=st.executeQuery(sql);catch(SQLExceptione)e.printStackTrace();finallyif(con!=null)trycon.close();catch(SQLExceptione)e.printStackTrace();6、假如需要記錄圖書的入庫時間(需要包括時分秒),應怎樣改正數(shù)據(jù)庫表構(gòu)造和有關(guān)代碼?【實驗結(jié)果與剖析】在bookbeak中增添:localdatepublicvoidcreateBook(BeanBookb)if(b.getBarcode()=null|b.getBarcode().length()20)thrownewBusinessException(throwsBaseException.equals(b.getBarcode()|條碼一定是1-20個字);if(b.getBookname()=null|b.getBookname().length()50)thrownewBusinessException(.equals(b.getBookname()|圖書名稱一定是1-50個字);Connectioncon

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論