《Web数据库学生实验报告JDBC部分》由会员分享,可在线阅读,更多相关《Web数据库学生实验报告JDBC部分(10页珍藏版)》请在金锄头文库上搜索。
1、Web数据库技术学生实验报告院系:信息科学与技术学院专业:信息管理与信息系统班 级:信 A1321/22任课教师:张海实验报告(一)院系:信息学院课程名称:Web数据库技术日期:班级A1321姓名黄伟峰专业信息管理与信息系统学号19实验室607实验名称Jdbc应用成绩评定教师签名实验目的1、掌握数据库驱动的加裁方式2、掌握connection对象的使用方法3、掌握statement对象使用方法4、掌握事务的处理机制5、掌握数据持久层的设计实验内容connection 对象、statement 对象 等应用1、请设计一个工程类通过配置文件如下db.properties来获得数据库连接的相关信息,
2、并通过该配置文件获得数据库连接对象。db.propertiesdriver=org.gjt.mm.mysql.Driverurl=jdbc:mysql:127.0.0.1/u5B66u751Fu5E93 userName=adminpwd=adminpublic class connectionFactory public static Connection getConnection() throws SQLException 请把getConnection ()方法补全。要求设计合理规范,必须有截图。 答案:public class connectionFactory privatesta
3、ticPropertiespros =new Properties()privatestaticStringdriver ;privatestaticStringurl ;privatestaticStringuserName ;privatestaticStringpwd ;staticInputstream is = connectionFactory.class .getClassLoader().getResourceAsStream( .properties);try pros .load(is);driver = pros .getProperty( driver url = pr
4、os .getProperty( url );user = pros .getProperty( userName pwd = pros .getProperty( pwd);Class. forName ( driver ); catch (Exception e) / TODO Auto-generated catch block e.printStackTrace();throws(url , user, pwd);public static Connection getConnection()SQLExceptionConnection conn = null ;try conn =
5、DriverManager. getConnection conn.setAutoCommit( false ); catch (Exception e) / TODO Auto-generated catch block e.printStackTrace();return conn ;2、已知学生定义如下:publicclassstudent privateint id ;privateStringstuld;privateStringname;privateStringdomCard;/楼栋宿舍号“31507”privateStringbedNo ; /床铺号publicstudent(
6、Stringstuld, Stringname, StringdomCard, StringbedNo)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=newArr
7、ayList();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 类的相关属性。id 学号 姓名 宿舍号 忘辆号Nu Nu L ;7u i i Nl I-.L u集合中的所有学生保持到学生表中
8、;(2) 通过 jdbc ,将 studentDatas 的 studentspublic 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
9、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.ex
10、ecuteUpdate(); mit(); catch (Exception e) try conn.rollback(); catch (SQLException e1) / TODOAuto-generated catch block e1.printStackTrace(); / TODO Auto-generated catch block e.printStackTrace(); finally if (prStatement != null ) try prStatement.cancel(); catch (SQLException e) / TODOAuto-generated
11、 catch block e.printStackTrace();if (conn != null ) try conn.close(); catch (SQLException e) / TODOAuto-generated catch block e.printStackTrace();class studentDataspublic static ArrayListstudents =newArrayList();static student stu= new student( 会计 A001121,张三,31 栋908 ,1);students .add(stu);stu= new s
12、tudent( 会计 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 / TODOAuto-generat
13、ed 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.executeUpdate(
14、) ;mit() ; catch (Exception e) try conn.rollback() ; catch (SQLException e1) / TODOAuto-generated catch block e1.printStackTrace();/ TODO Auto-generated catch block e.printStackTrace(); finally if (prStatement != null ) try prStatement.cancel() ; catch (SQLException e) / TODOAuto-generated catch block e.printStackTrace();if (conn != null ) try