ImageVerifierCode 换一换
格式:DOCX , 页数:14 ,大小:194.03KB ,
资源ID:6234794      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/6234794.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(数据库系统应用与开发实验四.docx)为本站会员(b****6)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

数据库系统应用与开发实验四.docx

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

2、ction 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(出版社编号已经被占用); rs.close(

3、); 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(出版社名称已经存在); rs.close(); st.close(); sql=insert into BeanPublisher(pubid,publ

4、isherName,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 DbExc

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

6、 rt) throws BaseException if(rt.getReaderTypeName()=null | .equals(rt.getReaderTypeName() | rt.getReaderTypeName().length()20) throw new BusinessException(读者类别名称必须是1-20个字); if(rt.getLendBookLimitted()100) throw new BusinessException(借阅图书数量必须在0-100之间); Connection conn=null; try conn=DBUtil.getConnect

7、ion(); 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(读者类别名称已经被占用); rs.close(); pst.close(); sql=sel

8、ect max(readerTypeId)from BeanReadertype; int i=1; pst=conn.prepareStatement(sql); rs = pst.executeQuery(); /id=rs.getint +1; if(!rs.next()/是否已经有id i=1; else i+=rs.getInt(1); sql=insert into BeanReaderType(readerTypeId,readerTypeName,lendBookLimitted) values(?,?,?); pst=conn.prepareStatement(sql); p

9、st.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); finally if(conn!=null) try conn.close(); catch (SQLException e) / TODO Auto-generated catch bl

10、ock e.printStackTrace(); 3、利用PreparedStatement对象进行数据修改。在SystemUserManager类中,新建一个modifyUserName方法,实现用户名称(username字段)的修改功能。并修改其main函数,将admin用户的名称改为:超级管理员。【实验结果与分析】A、请提供方法代码和main函数代码。public void modifyUserName(String username)throws BaseException Connection conn=null; try conn=DBUtil.getConnection(); S

11、tring 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); finally if(conn!=null) try conn.close(); catch (SQLException e) / TODO A

12、uto-generated catch block e.printStackTrace(); public static void main(String args) BeanSystemUser user=new BeanSystemUser(); user.setUserid(admin); user.setUsername(系统管理员); user.setUsertype(管理员); try new SystemUserManager().modifyUserName(超级管理员); catch (BaseException e) / TODO Auto-generated catch

13、block e.printStackTrace(); B、思考:如果上述方法的返回值为布尔类型,即如果成功修改了用户名称,则返回true,如果用户不存在或修改失败返回false。应该如何完善代码。提示:主要statement或PreparedStatement对象的execute方法和executeUpdate方法的区别。 public static void main(String args) BeanSystemUser user=new BeanSystemUser(); user.setUserid(admin); user.setUsername(系统管理员); user.setUs

14、ertype(管理员); try new SystemUserManager().modifyUserName(超级管理员1); catch (BaseException e) / TODO Auto-generated catch block e.printStackTrace(); public void modifyUserName(String username)throws BaseException Connection conn=null; try conn=DBUtil.getConnection(); String sql = update beansystemuser se

15、t 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); finally if(conn!=null) try conn.close(); catch

16、(SQLException e) / TODO Auto-generated catch block e.printStackTrace(); 4、Delete语句的执行。修改用户管理类中的用户删除方法,用删除数据库表中数据的形式代替现有软删除模式。【实验结果与分析】A、修改后的sql语句部分是。sql=delete BeanSystemUser where userid=?; /delete BeanSystemUser where userID=001B、如果对删除函数进行限制,要求不能删除已经有过借阅操作的用户。应如何修改代码。提示:可参考读者管理类中的读者类别删除方法。public v

17、oid deleteUser(String userid)throws BaseException Connection 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(!

18、rs.next() throw new BusinessException(登陆账号不 存在); if(rs.getDate(1)!=null) throw new BusinessException(该账号已经被删除); 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=dele

19、te BeanSystemUser where userID=?; pst=conn.prepareStatement(sql); pst.setString(1, userid); pst.execute(); pst.close(); else throw new BusinessException(该账号已有借阅信息,不能删除); catch (SQLException e) e.printStackTrace(); throw new DbException(e); finally if(conn!=null) try conn.close(); catch (SQLException

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

21、blic 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

22、.createStatement(); rs=st.executeQuery(sql); catch(SQLException e) e.printStackTrace(); finally if( con != null) try con.close(); catch (SQLException e) e.printStackTrace(); 6、如果需要记录图书的入库时间(需要包含时分秒),应如何修改数据库表结构和相关代码?【实验结果与分析】在bookbeak中添加:localdate public void createBook(BeanBook b) throws BaseExcept

23、ion if(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()

24、; String sql=select * from BeanBook where barcode=?; java.sql.PreparedStatement pst=conn.prepareStatement(sql); pst.setString(1, b.getBarcode(); java.sql.ResultSet rs=pst.executeQuery(); if(rs.next() throw new BusinessException(条码已经被占用); rs.close(); pst.close(); sql=insert into BeanBook(barcode,book

25、name,pubid,price,state,localdate) values(?,?,?,?,在库,?); pst=conn.prepareStatement(sql); pst.setString(1, b.getBarcode(); pst.setString(2, b.getBookname(); pst.setString(3, b.getPubid(); pst.setDouble(4, b.getPrice(); pst.setTimestamp(5, new java.sql.Timestamp(System.currentTimeMillis(); pst.execute(); pst.close(); catch (SQLException e) e.printStackTrace(); throw new DbException(e); finally if(conn!=null) try conn.close(); catch (SQLException e) / TODO Auto-generated catch block e.printStackTrace();

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1