




版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、實(shí)驗(yàn) JDBC基礎(chǔ)(3)一、相關(guān)知識(shí)點(diǎn)1、JDBC基本概念2、JDBC數(shù)據(jù)增、刪、改,事務(wù)控制等二、實(shí)驗(yàn)?zāi)康模豪斫釰ava連接數(shù)據(jù)庫(kù)的基本概念。理解利用Statement對(duì)象、PreparedStatement對(duì)象進(jìn)行增、刪、改操作,理解事務(wù)的概念和JDBC編程方式。三、實(shí)驗(yàn)內(nèi)容:1、 利用Statement對(duì)象進(jìn)行數(shù)據(jù)添加。第一步:修改PublisherManager類的createPublisher方法,將其中的insert語(yǔ)言改成用Statement對(duì)象執(zhí)行;第二步:運(yùn)行圖書(shū)管理系統(tǒng),進(jìn)行添加出版社測(cè)試?!緦?shí)驗(yàn)結(jié)果與分析】A、 寫(xiě)出替換的代碼部分。Connection conn=null
2、;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("出版社編
3、號(hào)已經(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.c
4、lose();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.getPubli
5、sherName();/st.setString(3,p.getAddress();st.execute(sql);st.close(); catch (SQLException e) e.printStackTrace();throw new DbException(e);2、 利用insert語(yǔ)句添加數(shù)據(jù)時(shí),未指定字段值處理。第一步:運(yùn)行圖書(shū)管理系統(tǒng),打開(kāi)讀者類別管理界面,并嘗試添加一個(gè)讀者類別;系統(tǒng)將會(huì)報(bào)一個(gè)錯(cuò)誤,請(qǐng)分析說(shuō)明錯(cuò)誤原因。reader.Typeid 是主碼 ,不能為空 第二步:通過(guò)數(shù)據(jù)庫(kù)表結(jié)構(gòu)的修改,解決上述問(wèn)題。并用同樣的方式解決圖書(shū)借閱功能的bug。打開(kāi) 企業(yè)管理器 ;打
6、開(kāi) beanreadertype 表;打開(kāi) 設(shè)計(jì)表;將 標(biāo)識(shí)改成 是 ; 第三步:如果表結(jié)構(gòu)不修改,應(yīng)該如何修改程序,使新增讀者類別的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(&qu
7、ot;讀者類別名稱必須是1-20個(gè)字");if(rt.getLendBookLimitted()<0 | rt.getLendBookLimitted()>100)throw new BusinessException("借閱圖書(shū)數(shù)量必須在0-100之間");Connection conn=null;try conn=DBUtil.getConnection();String sql="select * from BeanReaderType where readerTypeName=?"java.sql.PreparedState
8、ment 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(
9、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.getL
10、endBookLimitted();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對(duì)象進(jìn)行數(shù)據(jù)修改。在SystemUserManager類中,新建一個(gè)modif
11、yUserName方法,實(shí)現(xiàn)用戶名稱(username字段)的修改功能。并修改其main函數(shù),將admin用戶的名稱改為:超級(jí)管理員?!緦?shí)驗(yàn)結(jié)果與分析】A、 請(qǐng)?zhí)峁┓椒ùa和main函數(shù)代碼。public void modifyUserName(String username)throws BaseExceptionConnection conn=null;try conn=DBUtil.getConnection();String sql = "update beansystemuser set username = '"+username+"'
12、 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();publi
13、c static void main(String args)BeanSystemUser user=new BeanSystemUser();user.setUserid("admin");user.setUsername("系統(tǒng)管理員");user.setUsertype("管理員");try new SystemUserManager().modifyUserName("超級(jí)管理員"); catch (BaseException e) / TODO Auto-generated catch blocke.pr
14、intStackTrace();B、 思考:如果上述方法的返回值為布爾類型,即如果成功修改了用戶名稱,則返回true,如果用戶不存在或修改失敗返回false。應(yīng)該如何完善代碼。提示:主要statement或PreparedStatement對(duì)象的execute方法和executeUpdate方法的區(qū)別。public static void main(String args)BeanSystemUser user=new BeanSystemUser();user.setUserid("admin");user.setUsername("系統(tǒng)管理員");u
15、ser.setUsertype("管理員");try new SystemUserManager().modifyUserName("超級(jí)管理員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
16、 = "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
17、.printStackTrace();throw new DbException(e);finallyif(conn!=null)try conn.close(); catch (SQLException e) / TODO Auto-generated catch blocke.printStackTrace();4、 Delete語(yǔ)句的執(zhí)行。修改用戶管理類中的用戶刪除方法,用刪除數(shù)據(jù)庫(kù)表中數(shù)據(jù)的形式代替現(xiàn)有軟刪除模式?!緦?shí)驗(yàn)結(jié)果與分析】A、修改后的sql語(yǔ)句部分是。sql="delete BeanSystemUser where userid=?" /delete Be
18、anSystemUser where userID='001'B、如果對(duì)刪除函數(shù)進(jìn)行限制,要求不能刪除已經(jīng)有過(guò)借閱操作的用戶。應(yīng)如何修改代碼。提示:可參考讀者管理類中的讀者類別刪除方法。public void deleteUser(String userid)throws BaseExceptionConnection conn=null;try conn=DBUtil.getConnection();String sql="select removeDate from BeanSystemUser where userid=?"java.sql.Prepa
19、redStatement pst=conn.prepareStatement(sql);pst.setString(1,userid);java.sql.ResultSet rs=pst.executeQuery();if(!rs.next() throw new BusinessException("登陸賬號(hào)不 存在");if(rs.getDate(1)!=null) throw new BusinessException("該賬號(hào)已經(jīng)被刪除");rs.close();pst.close();sql="select * from BeanBo
20、okLendRecord 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(&qu
21、ot;該賬號(hào)已有借閱信息,不能刪除"); 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ù)庫(kù)中建立一張BeanBookLendRecord_backup表,用于保存已經(jīng)歸還圖書(shū)的借閱記錄。其表結(jié)構(gòu)與BeanBookLendRecord表完全一致。要求在借閱管理
22、類中,增加方法,實(shí)現(xiàn)已經(jīng)歸還數(shù)據(jù)的備份功能(備份完成后,在原表中刪除備份成功的數(shù)據(jù))。提示:注意事務(wù)控制?!緦?shí)驗(yàn)結(jié)果與分析】A 請(qǐng)?zhí)峁﹤浞荼淼慕ū碚Z(yǔ)句select * into BeanBookLendRecord_backup from BeanBookLendRecordB 請(qǐng)?zhí)峁﹤浞莺瘮?shù)代碼public void a() java.sql.Connection con = null;try con = DBUtil.getConnection();String sql = "insert into BeanBookLendRecord_backup select * from
23、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
24、 (SQLException e)e.printStackTrace(); 6、 如果需要記錄圖書(shū)的入庫(kù)時(shí)間(需要包含時(shí)分秒),應(yīng)如何修改數(shù)據(jù)庫(kù)表結(jié)構(gòu)和相關(guān)代碼?【實(shí)驗(yàn)結(jié)果與分析】在bookbeak中添加:localdate public void createBook(BeanBook b) throws BaseExceptionif(b.getBarcode()=null | "".equals(b.getBarcode() | b.getBarcode().length()>20)throw new BusinessException("條碼必須是1-20個(gè)字");if(b.getBookname()=null | "".equals(b.getBookname() | b.getBookname().length()>50)throw new BusinessException("圖書(shū)名稱必須是1-50個(gè)字");Connection conn=null;try conn=DBUtil.getConnection();St
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 廠房股權(quán)轉(zhuǎn)讓與新能源產(chǎn)業(yè)合作開(kāi)發(fā)合同
- 商業(yè)地產(chǎn)項(xiàng)目場(chǎng)地標(biāo)準(zhǔn)租賃協(xié)議
- 鄰居砸墻協(xié)議書(shū)范本
- 果園采摘園承包與果樹(shù)修剪技術(shù)服務(wù)合同
- 風(fēng)險(xiǎn)防范叉車操作工勞動(dòng)合同
- 高端餐飲企業(yè)員工勞動(dòng)合同范本(含績(jī)效考核)
- 分公司投資建設(shè)與產(chǎn)業(yè)園區(qū)合作協(xié)議
- 中醫(yī)情志與健康的關(guān)系
- AI教育智適應(yīng)系統(tǒng)應(yīng)用與發(fā)展
- 珍愛(ài)生命健康成長(zhǎng)幼兒園
- 2025年三級(jí)電子商務(wù)師職業(yè)技能鑒定理論考試題庫(kù)(含答案)
- 帶電作業(yè)安全課件
- 2025年糧油倉(cāng)儲(chǔ)管理員職業(yè)技能競(jìng)賽參考試題庫(kù)(含答案)
- 白蟻防治文保施工方案
- 2025年中國(guó)國(guó)際技術(shù)智力合作集團(tuán)有限公司招聘筆試參考題庫(kù)含答案解析
- 2025時(shí)政試題及答案(100題)
- 食品安全自查、從業(yè)人員健康管理、進(jìn)貨查驗(yàn)記錄、食品安全事故處置等保證食品安全的規(guī)章制度
- 國(guó)家保密知識(shí)培訓(xùn)課件
- 臨床科主任考核管理辦法
- 社交媒體表情符號(hào)分析
- 農(nóng)村文化產(chǎn)業(yè)發(fā)展與市場(chǎng)開(kāi)拓策略
評(píng)論
0/150
提交評(píng)論