数据库系统应用与开发实验四_第1页
数据库系统应用与开发实验四_第2页
数据库系统应用与开发实验四_第3页
数据库系统应用与开发实验四_第4页
已阅读5页,还剩2页未读 继续免费阅读

付费下载

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、实验JDBC 基础( 3)一、相关知识点1、JDBC 基本概念2、JDBC 数据增、删、改,事务控制等二、实验目的:理解 Java 连接数据库的基本概念。理解利用 Statement 对象、 PreparedStatement 对象进行增、删、改操作,理解事务的概念和 JDBC 编程方式。三、实验内容:1、 利用 Statement 对象进行数据添加。第一步:修改PublisherManager类的createPublisher 方法,将其中的insert语言改成用Statement 对象执行;第二步:运行图书管理系统,进行添加出版社测试。【实验结果与分析】A 、写出替换的代码部分。Conne

2、ction conn=null;tryconn=DBUtil.getConnection();String sql="select * from BeanPublisher where pubid='"conn.createStatement();/st.setString(1,p.getPubid();if(rs.next()thrownew BusinessException(rs.close();st.close();sql="select * from BeanPublisher wherepublisherName='"+p.g

3、etPublisherName()+"'"+p.getPubid()+" 出版社编号已经被占用" );"'"st=conn.createStatement();/ st.setString(1, p.getPublisherName(); rs=st.executeQuery(sql);if(rs.next()thrownew BusinessException(rs.close();st.close();" 出版社名称已经存在" );sql="insert into BeanPublis

4、her(pubid,publisherName,address)values('"+p.getPubid()+"','"+p.getPublisherName()+"','"st=conn.createStatement();/st.setString(1, p.getPubid();/st.setString(2, p.getPublisherName();/st.setString(3,p.getAddress();st.execute(sql);st.close();catch(SQLExcepti

5、on e) e.printStackTrace();thrownew DbException(e);+p.getAddress()+"')"2、 利用 insert 语句添加数据时,未指定字段值处理。第一步:运行图书管理系统,打开读者类别管理界面,并尝试添加一个读者类别;系统将会报一个错误,请分析说明错误原因。reader.Typeid是主码,不能为空第二步:通过数据库表结构的修改,解决上述问题。并用同样的方式解决图书借阅功能的bug。打开企业管理器;打开beanreadertype 表;打开设计表;将标识改成是;第三步:如果表结构不修改,应该如何修改程序,使新增读

6、者类别的ID 为表中现有数据的最大ID值 +1。publicvoidcreateReaderType(BeanReaderType rt)if(rt.getReaderTypeName()=nullrt.getReaderTypeName().length()>20)thrownew BusinessException(throwsBaseException|"" .equals(rt.getReaderTypeName() |" 读者类别名称必须是1-20 个字 " );if(rt.getLendBookLimitted()<0 | rt.

7、getLendBookLimitted()>100)thrownew BusinessException(" 借阅图书数量必须在0-100之间");Connection conn=null;tryconn=DBUtil.getConnection();String sql="select * from BeanReaderType where readerTypeName=?"pst.setString(1, rt.getReaderTypeName();if(rs.next()thrownew BusinessException(" 读

8、者类别名称已经被占用rs.close();pst.close();sql="select max(readerTypeId)from BeanReadertype"inti=1;pst=conn.prepareStatement(sql);rs = pst.executeQuery();/id=rs.getint +1;if(!rs.next()/是否已经有idi=1;" );elsei+=rs.getInt(1);sql="insert intoBeanReaderType(readerTypeId,readerTypeName,lendBookLim

9、itted) 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();thrownew DbException(e);finallyif(conn!=null)tryconn.close();catch(SQ

10、LException e) /TODO Auto-generated catch blocke.printStackTrace();3、 利用 PreparedStatement 对象进行数据修改。在 SystemUserManager 类中,新建一个modifyUserName 方法,实现用户名称(功能。并修改其main 函数,将admin 用户的名称改为:超级管理员。username 字段)的修改【实验结果与分析】A 、请提供方法代码和main 函数代码。publicvoidmodifyUserName(String username)throwsBaseExceptionConnecti

11、on conn=null;tryconn=DBUtil.getConnection();String sql ="update beansystemuser set username = '"userid='admin'"intrs = st.executeUpdate(sql);catch(SQLException e) e.printStackTrace();thrownew DbException(e);+username+"' wherefinallytryconn.close();catch(SQLExcepti

12、on e) /TODO Auto-generated catch blocke.printStackTrace();publicstaticvoidmain(String args)BeanSystemUser user=new BeanSystemUser();user.setUserid("admin");user.setUsername(" 系统管理员" );user.setUsertype("管理员 ");trynew SystemUserManager().modifyUserName(catch(BaseException

13、 e) /TODO Auto-generated catch block" 超级管理员" );e.printStackTrace();B、思考:如果上述方法的返回值为布尔类型,即如果成功修改了用户名称,则返回true,如果用户不存在或修改失败返回false。应该如何完善代码。提示:主要statement 或 PreparedStatement 对象的execute 方法和 executeUpdate 方法的区别。publicstaticvoidmain(String args)BeanSystemUser user=new BeanSystemUser();user.set

14、Userid("admin");user.setUsername(" 系统管理员" );user.setUsertype("管理员 ");trynew SystemUserManager().modifyUserName(" 超级管理员1" );catch(BaseException e) / TODO Auto-generated catch block e.printStackTrace();publicvoidmodifyUserName(String username)throwsBaseException

15、Connection conn=null;tryconn=DBUtil.getConnection();String sql ="update beansystemuser set username = '"+username+"' whereuserid='admin'"booleanrs = st.execute(sql);if(rs =true)System.out .print("ok");elseSystem.out .print("no");catch(SQLExcept

16、ion e) e.printStackTrace();thrownew DbException(e);finallyif(conn!=null)tryconn.close();catch(SQLException e) / TODO Auto-generated catch block e.printStackTrace();4、 Delete 语句的执行。修改用户管理类中的用户删除方法,用删除数据库表中数据的形式代替现有软删除模式。【实验结果与分析】A 、修改后的sql 语句部分是。sql="deleteBeanSystemUserwhereuserid=?"/delet

17、eBeanSystemUserwhereuserID='001'B、如果对删除函数进行限制,要求不能删除已经有过借阅操作的用户。应如何修改代码。提示:可参考读者管理类中的读者类别删除方法。publicvoiddeleteUser(String userid)throwsBaseExceptionConnection conn=null;tryconn=DBUtil.getConnection();String sql="select removeDate from BeanSystemUser where userid=?"pst.setString(1,u

18、serid);if(!rs.next()thrownewBusinessException(" 登陆账号不存在");if(rs.getDate(1)!=null)thrownew BusinessException(" 该账号已经被删除 " );rs.close();pst.close();sql="select * from BeanBookLendRecord where lendOperUserid=?"pst=conn.prepareStatement(sql);pst.setString(1,userid);rs=pst.e

19、xecuteQuery();if(!rs.next()sql="delete BeanSystemUser where userID=?"pst=conn.prepareStatement(sql);pst.setString(1, userid);pst.execute();pst.close();elsethrownew BusinessException(" 该账号已有借阅信息, 不能删除 " ); catch(SQLException e) e.printStackTrace();thrownew DbException(e);finallyif

20、(conn!=null)try conn.close();catch(SQLException e) / TODO Auto-generated catch block e.printStackTrace();5、 在数据库中建立一张BeanBookLendRecord_backup表,用于保存已经归还图书的借阅记录。其表结构与BeanBookLendRecord 表完全一致。要求在借阅管理类中,增加方法,实现已经归还数据的备份功能(备份完成后,在原表中删除备份成功的数据) 。提示:注意事务控制。【实验结果与分析】A 请提供备份表的建表语句select * into BeanBookLendR

21、ecord_backup from BeanBookLendRecordB 请提供备份函数代码publicvoida()null;trycon = DBUtil.getConnection();String sql ="insert into BeanBookLendRecord_backup select * fromBeanBookLendRecord"createStatement();rs = st.executeQuery(sql);sql="delete from BeanBookLendRecord"st=con.createStateme

22、nt();rs=st.executeQuery(sql); catch(SQLException e)e.printStackTrace();finallyif( con !=null)trycon.close();catch(SQLException e)e.printStackTrace();6、 如果需要记录图书的入库时间(需要包含时分秒) ,应如何修改数据库表结构和相关代码?【实验结果与分析】在 bookbeak 中添加: localdatepublicvoidcreateBook(BeanBook b)if(b.getBarcode()=null|b.getBarcode().length()>20)thrownew BusinessException(throwsBaseException"" .equals(b.getBarcode() |" 条码必须是 1-20个字 " );if(b.getBookname()=null|b.getBookname().length()>50)thrownew BusinessException("" .equals(b.getBookname() |" 图书名称必须是1

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论