Web数据库学生实验报告JDBC部分

上传人:枫** 文档编号:498339396 上传时间:2022-11-11 格式:DOC 页数:11 大小:61KB
返回 下载 相关 举报
Web数据库学生实验报告JDBC部分_第1页
第1页 / 共11页
Web数据库学生实验报告JDBC部分_第2页
第2页 / 共11页
Web数据库学生实验报告JDBC部分_第3页
第3页 / 共11页
Web数据库学生实验报告JDBC部分_第4页
第4页 / 共11页
Web数据库学生实验报告JDBC部分_第5页
第5页 / 共11页
点击查看更多>>
资源描述

《Web数据库学生实验报告JDBC部分》由会员分享,可在线阅读,更多相关《Web数据库学生实验报告JDBC部分(11页珍藏版)》请在金锄头文库上搜索。

1、Web数据库技术学生实验报告 院 系: 信息科学与技术学院专 业: 信息管理与信息系统班 级: 信A1321/22任课教师: 张海 实验报告(一)院系:信息学院 课程名称:Web数据库技术 日期:班 级A1321姓 名黄伟峰专 业信息管理与信息系统学 号19实 验 室607实验名称Jdbc应用成 绩 评 定教 师 签 名实验目的1、 掌握数据库驱动的加载方式2、 掌握connection对象的使用方法3、 掌握statement对象使用方法4、 掌握事务的处理机制5、掌握数据持久层的设计实验内容connection对象、statement对象等应用1、 请设计一个工程类通过配置文件如下db.p

2、roperties来获得数据库连接的相关信息,并通过该配置文件获得数据库连接对象。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 connecti

3、onFactory 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) ;dri

4、ver = 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 =

5、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

6、-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

7、 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)

8、通过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 fo

9、r(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() ;p

10、rStatement.executeUpdate(); mit() ; 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 Au

11、to-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); st

12、u=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

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 建筑/环境 > 施工组织

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号