版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、精选优质文档-倾情为你奉上实验 JDBC基础(3)一、相关知识点1、JDBC基本概念2、JDBC数据增、删、改,事务控制等二、实验目的:理解Java连接数据库的基本概念。理解利用Statement对象、PreparedStatement对象进行增、删、改操作,理解事务的概念和JDBC编程方式。三、实验内容:1、 利用Statement对象进行数据添加。第一步:修改PublisherManager类的createPublisher方法,将其中的insert语言改成用Statement对象执行;第二步:运行图书管理系统,进行添加出版社测试。【实验结果与分析】A、 写出替换的代码部分。Connect
2、ion 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 BusinessExcepti
3、on("出版社编号已经被占用");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("出版社名称已经存
4、在");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(
5、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 是主码 ,不能为空 第二步:通过数据库表结构的修改,解决上述问题。并用同样的方式解决图书借阅功能的bu
6、g。打开 企业管理器 ;打开 beanreadertype 表;打开 设计表;将 标识改成 是 ; 第三步:如果表结构不修改,应该如何修改程序,使新增读者类别的ID为表中现有数据的最大ID值+1。public void createReaderType(BeanReaderType rt) throws BaseExceptionif(rt.getReaderTypeName()=null | "".equals(rt.getReaderTypeName() | rt.getReaderTypeName().length()>20)throw new Business
7、Exception("读者类别名称必须是1-20个字");if(rt.getLendBookLimitted()<0 | rt.getLendBookLimitted()>100)throw new BusinessException("借阅图书数量必须在0-100之间");Connection conn=null;try conn=DBUtil.getConnection();String sql="select * from BeanReaderType where readerTypeName=?"java.sql.
8、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.prep
9、areStatement(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.set
10、Int(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对象进行数据修改。在SystemUserManage
11、r类中,新建一个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 = '"+usernam
12、e+"' 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.printStack
13、Trace();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 ca
14、tch blocke.printStackTrace();B、 思考:如果上述方法的返回值为布尔类型,即如果成功修改了用户名称,则返回true,如果用户不存在或修改失败返回false。应该如何完善代码。提示:主要statement或PreparedStatement对象的execute方法和executeUpdate方法的区别。public static void main(String args)BeanSystemUser user=new BeanSystemUser();user.setUserid("admin");user.setUsername("系
15、统管理员");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
16、();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 (SQLE
17、xception 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=?&quo
18、t; /delete BeanSystemUser where userID='001'B、如果对删除函数进行限制,要求不能删除已经有过借阅操作的用户。应如何修改代码。提示:可参考读者管理类中的读者类别删除方法。public void deleteUser(String userid)throws BaseExceptionConnection conn=null;try conn=DBUtil.getConnection();String sql="select removeDate from BeanSystemUser where userid=?"j
19、ava.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
20、* 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 Business
21、Exception("该账号已有借阅信息,不能删除"); 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
22、表完全一致。要求在借阅管理类中,增加方法,实现已经归还数据的备份功能(备份完成后,在原表中删除备份成功的数据)。提示:注意事务控制。【实验结果与分析】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 s
23、elect * 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.c
24、lose(); 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 s
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2024年南通市启东市医疗卫生单位招聘考试真题
- 课程设计模式目标
- 安卓课程设计 秒表
- 2025年杭州极弱磁场重大科技基础设施研究院校园招聘备考题库及一套参考答案详解
- ios通讯录课程设计
- 2025年中国航空工业集团有限公司招聘备考题库及完整答案详解一套
- 2025年中国科学院半导体研究所科技管理与成果处科研业务主管招聘备考题库及一套答案详解
- 2025河北秦皇岛市社会保险事业服务中心选调工作人员6名笔试重点题库及答案解析
- 2025年绍兴市文化市场执法指导中心招聘编制外工作人员备考题库及一套答案详解
- 2025年在线问诊医师资质十年认证:分级管理与行业创新行业报告
- 2025年1月电大国家开放大学期末试题及答案:创业基础
- 粤语文化课件教学
- 电梯装卸方案模板(3篇)
- 消防档案全套表格模板
- 新能源汽车产品开发流程试题及答案
- 研究生教育质量保障体系建设
- 语言表达的修辞解码知到课后答案智慧树章节测试答案2025年春湘潭大学
- 华为灰度管理法读书分享
- 搅拌站安装施工方案
- 山东省济南市钢城区2024-2025学年八年级上学期1月期末化学试题(原卷版+解析版)
- 湖北省武汉市江岸区2024-2025学年九年级上学期期末考试英语试题(含答案无听力原文及音频)
评论
0/150
提交评论