资源预览内容
第1页 / 共11页
第2页 / 共11页
第3页 / 共11页
第4页 / 共11页
第5页 / 共11页
第6页 / 共11页
第7页 / 共11页
第8页 / 共11页
第9页 / 共11页
第10页 / 共11页
亲,该文档总共11页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述
Web数据库技术学生实验报告 院 系: 信息科学与技术学院专 业: 信息管理与信息系统班 级: 信A1321/22任课教师: 张海 实验报告(一)院系:信息学院 课程名称:Web数据库技术 日期:班 级A1321姓 名黄伟峰专 业信息管理与信息系统学 号19实 验 室607实验名称Jdbc应用成 绩 评 定教 师 签 名实验目的1、 掌握数据库驱动的加载方式2、 掌握connection对象的使用方法3、 掌握statement对象使用方法4、 掌握事务的处理机制5、掌握数据持久层的设计实验内容connection对象、statement对象等应用1、 请设计一个工程类通过配置文件如下db.properties来获得数据库连接的相关信息,并通过该配置文件获得数据库连接对象。db.propertiesdriver=org.gjt.mm.mysql.Driverurl=jdbc:mysql:/127.0.0.1/u5B66u751Fu5E93userName=adminpwd=adminpublic class connectionFactory public static Connection getConnection() throws SQLException请把getConnection()方法补全。要求设计合理规范,必须有截图。答案:public class connectionFactory private static Properties pros = new Properties() ;private static String driver ;private static String url ;private static String userName ;private static String pwd ;static InputStream is = connectionFactory.class.getClassLoader().getResourceAsStream(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 blocke.printStackTrace();public static Connection getConnection() throws SQLExceptionConnection conn = null ;try conn = DriverManager.getConnection(url, user, pwd) ;conn.setAutoCommit(false) ; catch (Exception e) / TODO Auto-generated catch blocke.printStackTrace();return conn ;2、 已知学生定义如下:public class student private int id; private String stuId; private String name; private String domCard;/楼栋宿舍号“31-507”private String bedNo;/床铺号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 studentDataspublic static ArrayList students=new ArrayList();staticstudent stu=new student(会计A001121,张三,31栋908,1); students.add(stu); stu=new student(会计A001166,李四,31栋908,2); students.add(stu); stu=new student(会计A001177,王五,31栋807,4); students.add(stu); 现要求 (1) 根据student类建立一个学生表用来保存student类的相关属性。(2) 通过jdbc,将studentDatas的students集合中的所有学生保持到学生表中; public class StuAdd /* * param args * throws SQLException */public static void main(String args) throws SQLException studentDatas stus = new studentDatas() ;Connection conn = connectionFactory.getConnection() ;PreparedStatement prStatement = null ;try for(student stu:stus.students)/增加String sql=insert into lianxi(学号,姓名,宿舍号,床铺号) values(?,?,?,?);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(); conn.commit() ; catch (Exception e) try conn.rollback() ; catch (SQLException e1) / TODO Auto-generated catch blocke1.printStackTrace();/ TODO Auto-generated catch blocke.printStackTrace();finallyif (prStatement != null) try prStatement.cancel() ; catch (SQLException e) / TODO Auto-generated catch blocke.printStackTrace();if (conn != null) try conn.close() ; catch (SQLException e) / TODO Auto-generated catch blocke.printStackTrace();class studentDataspublic static ArrayList students=new ArrayList();static student stu=new student(会计A001121,张三,31栋908,1); students.add(stu); stu=new student(会计A001166,李四,31栋908,2); students.add(stu); stu=new student(会计A001177,王五,31栋807,4); students.add(stu); (3) 通过jdbc,将学生表中所有的宿舍是” 31栋908”学生全部调整到“20栋371”宿舍;public class StuAlter /* * param args * throws SQLException */public static void main(String args) throws SQLException / TODO Auto-generated method stubConnection conn = connectionFactory.getConnection() ;PreparedStatement prStatement = null ;try /修改String sql = update lianxi set 宿舍号 =? where 宿舍号=? ;prStatement = conn.prepareStatement(sql) ;prStatement.setString(1, 20栋371) ;prStatement.setString(2, 31栋908) ;prStatement.exe
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号