




已阅读5页,还剩8页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
。实验 JDBC基础(3)一、相关知识点1、JDBC基本概念2、JDBC数据增、删、改,事务控制等二、实验目的:理解Java连接数据库的基本概念。理解利用Statement对象、PreparedStatement对象进行增、删、改操作,理解事务的概念和JDBC编程方式。三、实验内容:1、 利用Statement对象进行数据添加。第一步:修改PublisherManager类的createPublisher方法,将其中的insert语言改成用Statement对象执行;第二步:运行图书管理系统,进行添加出版社测试。【实验结果与分析】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(出版社编号已经被占用);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(出版社名称已经存在);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语句添加数据时,未指定字段值处理。第一步:运行图书管理系统,打开读者类别管理界面,并尝试添加一个读者类别;系统将会报一个错误,请分析说明错误原因。reader.Typeid 是主码 ,不能为空 第二步:通过数据库表结构的修改,解决上述问题。并用同样的方式解决图书借阅功能的bug。打开 企业管理器 ;打开 beanreadertype 表;打开 设计表;将 标识改成 是 ; 第三步:如果表结构不修改,应该如何修改程序,使新增读者类别的ID为表中现有数据的最大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(借阅图书数量必须在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(读者类别名称已经被占用);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()/是否已经有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对象进行数据修改。在SystemUserManager类中,新建一个modifyUserName方法,实现用户名称(username字段)的修改功能。并修改其main函数,将admin用户的名称改为:超级管理员。【实验结果与分析】A、 请提供方法代码和main函数代码。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(系统管理员);user.setUsertype(管理员);try new SystemUserManager().modifyUserName(超级管理员); catch (BaseException e) / TODO Auto-generated catch blocke.printStackTrace();B、 思考:如果上述方法的返回值为布尔类型,即如果成功修改了用户名称,则返回true,如果用户不存在或修改失败返回false。应该如何完善代码。提示:主要statement或PreparedStatement对象的execute方法和executeUpdate方法的区别。public static void main(String args)BeanSystemUser user=new BeanSystemUser();user.setUserid(admin);user.setUsername(系统管理员);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语句的执行。修改用户管理类中的用户删除方法,用删除数据库表中数据的形式代替现有软删除模式。【实验结果与分析】A、修改后的sql语句部分是。sql=delete BeanSystemUser where userid=?; /delete BeanSystemUser where userID=001B、如果对删除函数进行限制,要求不能删除已经有过借阅操作的用户。应如何修改代码。提示:可参考读者管理类中的读者类别删除方法。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(该账号已经被删除);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、 在数据库中建立一张BeanBookLendRecord_backup表,用于保存已经归还图书的借阅记录。其表结构与BeanBookLendRecord表完全一致。要求在借阅管理类中,增加方法,实现已经归还数据的备份功能(备份完成后,在原表中删除备份成功的数据)。提示:注意事务控制。【实验结果与分析】A 请提供备份表的建表语句select * into BeanBookLendRecord_backup from BeanBookLendRecordB 请提供备份函数代码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、 如果需要记录图书的入库时间(需要包含时分秒),应如何修改数据库表结构和相关代码?【实验结果与分析】在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 BeanBook where barcode=?;java.sql.PreparedStatement pst=
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 红十字理论试题及答案
- DB6105T 180-2022 大豆种子生产田间检验技术规程
- 查对制度试题(含答案)
- 家具智能安防课件
- 安全培训登记制度课件
- 排油烟工程方案(3篇)
- 受伤赔偿协议书格式5篇
- 2025年特种机载装置项目立项申请报告模板
- 隧道工程通风方案(3篇)
- 2024年潍坊市产业发展集团招聘真题及一套参考答案详解
- 《生活中的数学》课件
- 书虫系列-绿野仙踪The+Wizard+of+Oz课件
- 短暂性脑缺血发作护理查房2021
- 鲁班奖创优方案
- DB14-T 2811-2023 液态排渣粉煤气化原料煤评价规范
- 厦门航空地面岗位应聘报名表
- PPT稻草人读书分享
- 资金供应计划及保障措施
- 建筑装饰造型设计基础-完整全套教学课件
- 过程装备与控制工程专业导论
- 2022版义务教育数学新课程标准新课标重点知识整理
评论
0/150
提交评论