1、专 业信息管理与信息系统学 号19实 验 室607实验名称Jdbc应用成 绩 评 定教 师 签 名实验目的1、 掌握数据库驱动的加载方式2、 掌握connection对象的使用方法3、 掌握statement对象使用方法4、 掌握事务的处理机制5、掌握数据持久层的设计实验内容connection对象、statement对象等应用1、 请设计一个工程类通过配置文件如下db.properties来获得数据库连接的相关信息,并通过该配置文件获得数据库连接对象。db.propertiesdriver=url=jdbc:userName=adminpwd=adminpublic class connec
2、tionFactory public static Connection getConnection() throws SQLException 请把getConnection()方法补全。要求设计合理规范,必须有截图。答案: private static Properties pros = new Properties() ; private static String driver ; private static String url ; private static String userName ; private static String pwd ; static InputSt
3、ream is = db.properties) ; try pros.load(is) ; driver = pros.getProperty(driver url = pros.getProperty(url user = pros.getProperty(userName pwd = pros.getProperty(pwd Class.forName(driver) ; catch (Exception e) / TODO Auto-generated catch block e.printStackTrace(); public static Connection getConnec
4、tion() throws SQLException Connection conn = null ; conn = DriverManager.getConnection(url, user, pwd) ; conn.setAutoCommit(false) ; return conn ;2、 已知学生定义如下:public class student private int id; private String stuId; private String name; private String domCard;/楼栋宿舍号“31-507”private String bedNo;/床铺号
5、 public student(String stuId, String name, String domCard, String bedNo) super(); this.stuId = stuId; this.name = name; this.domCard = domCard; this.bedNo = bedNo;public String toString()return id=+id+;学号=+stuId+”;姓名=”+name+”;宿舍号=”+domCard+”;床铺号=”+bedNo;/相应get、set方法省略有一class studentDatas public stat
6、ic ArrayList students=new ArrayList(); static student stu=new student(会计A001121,张三31栋908,1); students.add(stu); stu=new student(会计A001166李四,2);会计A001177王五31栋807,4);现要求 (1) 根据student类建立一个学生表用来保存student类的相关属性。(2) 通过jdbc,将studentDatas的students集合中的所有学生保持到学生表中;public class StuAdd /* * param args * throws
7、 SQLException */ public static void main(String args) throws SQLException studentDatas stus = new studentDatas() ; Connection conn = connectionFactory.getConnection() ; PreparedStatement prStatement = null ; for(student stu:stus.students) /增加 String sql=insert into lianxi(学号,姓名,宿舍号,床铺号) values(?,?)
8、prStatement = conn.prepareStatement(sql) ; prStatement.setString(1, stu.getStuId() ; prStatement.setString(2, stu.getName() ; prStatement.setString(3, stu.getDomCard() ; prStatement.setString(4, stu.getBedNo() ; prStatement.executeUpdate(); mit() ; try conn.rollback() ; catch (SQLException e1) / TOD
9、O Auto-generated catch block e1.printStackTrace(); finally if (prStatement != null) try prStatement.cancel() ; catch (SQLException e) / TODO Auto-generated catch block e.printStackTrace(); if (conn ! conn.close() ;1);24(3) 通过jdbc,将学生表中所有的宿舍是” 31栋908”学生全部调整到“20栋371”宿舍;public class StuAlter / TODO Auto-generated method stub /修改 String sql = update lianxi set 宿舍号 =? where 宿舍号=? ; prStatement = conn.prepareStatement(sql) ; prStatement.setString(1, 20栋371 prStatement.setString(2, prStatement.executeUpdate() ; mit() ;
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1