Web数据库学生实验报告JDBC部分_第1页
Web数据库学生实验报告JDBC部分_第2页
Web数据库学生实验报告JDBC部分_第3页
Web数据库学生实验报告JDBC部分_第4页
Web数据库学生实验报告JDBC部分_第5页
已阅读5页,还剩5页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

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

2、并通过该配置文件获得数据库连接对象。pertiesdriver=org.gjt.mm.mysql.Driverurl=jdbc:mysql:/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; = 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 blo

15、ck e.printStackTrace();if (conn != null ) try conn.close(); catch (SQLException e) / TODOAuto-generated catch block e.printStackTrace();(4)通过jdbc ,删除31栋807床铺号是4的学生。public class StuDeletion /* param args* throws SQLException* /public static void main(String args) throws SQLException / TODOAuto-genera

16、ted method stubList students= new ArrayList();Connection conn = connectionFactory.getConnection ();ResultSet rs = null ;PreparedStatement prStatement =null ;try /删除String sql = delete from lianxi where宿舍号=31 栋807 and 床铺号=4;prStatement=conn.prepareStatement(sql);prStatement.executeUpdate();mit(); cat

17、ch (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.printStac

18、kTrace();if (conn != null ) try conn.close() ; catch (SQLException e) / TODOAuto-generated catch block e.printStackTrace();3、ResultSet对象操作请将上题中宿舍号最后一位是“8”的学生信息全部显示出来。public class StuQuery /* param args* throws SQLException*/public static void main(String args) throws SQLException / TODOAuto-generate

19、d method stubList students=new ArrayList();Connection conn = connectionFactory.getConnection ();ResultSet rs = null ;PreparedStatement prStatement =null ;student stu = null ; try String sql = select lianxi.* from lianxi where宿舍号 like %8;prStatement = conn.prepareStatement(sql);rs = prStatement.executeQuery(sql);while (rs.next() stu= new student(sql, sql, sql, sql);stu.setld(rs.getlnt(id);stu.setStuId(rs.getString(学号);stu.setName(rs.getString(姓名);stu.setDomCard(rs.getString(宿舍号);stu.setBedNo(rs.getString(床铺号);students.add(stu);System. out

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论