JDBC与Servlet实验报告

发布时间 : 星期四 文章JDBC与Servlet实验报告更新完毕开始阅读

实验报告(二)

院系:信息学院 课程名称:Web数据库技术 日期: 班 级 专 业 姓 名 学 号 成 绩 评 定 实验名称 Jdbc应用 教 师 签 名 实 验 室 实验目的 1、 掌握数据库驱动的加载方式 2、 掌握connection对象的使用方法 3、 掌握statement对象使用方法 4、 掌握事务的处理机制 5、掌握数据持久层的设计 实验内容 connection对象、statement对象等应用

一、 1、 请建立一个与本地mysql服务器的“信息库”的连接对象并输出该连接。 public class Factory { private static String driver; private static String url; private static String username; private static String password; 实验过程、结果及分析 private static Connection conn; //private static Properties pro; //private static Properties p1; static { driver=” org.gjt.mm.mysql.Driver” url=” jdbc:mysql://127.0.0.1/学生库” username=” root” password=”123” } public static Connection getconnection() throws ClassNotFoundException, FileNotFoundException{ try { Class.forName(driver); conn=DriverManager.getConnection(url,username,password); conn.setAutoCommit(false); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return conn; } public static void main(String[] args) { try { System.out.println(Factory.getconnection()); System.out.println(\连接成功!\); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } 2, 请将成绩表中所有学号是’B030110’的成绩加10分 Public class addgrade{ Public static void main(String args[]){ String sql=”update grade set scores=scores+10 where sno=?” Preparedstatement state=null; 二、 1、请通过preparedstatement在学生表中输入两个学生信息 Studente stu=new students(); Students stu1=new students(); String sql=\学生表 values(?,?,?)\; PreparedStatement state=null; Connection con=null; 二、1、Stu 实验过程、结果及分析 con=Factory.getconnection(); state=con.prepareStatement(sql); state1=con.prepareStatement(sql) state.setObject(1,1); state.setNString(2,stu.getname()); state.setNString(3,stu.getxuehao()); state.executeQuery(); state1.setObject(1,2); state1.setNString(2,stu1.getname()); state1.setNString(3,stu1.getxuehao()); state.executeQuery(); conn.commit(); 2、请显示学生表中所有学生的信息 public class test { public static void main(String[] args) throws ClassNotFoundException, SQLException, FileNotFoundException { String sql1=\学生表”; Connection con=null; PreparedStatement state=null; con=Factory.getconnection(); state=con.prepareStatement(sql1); ResultSet rs=state.executeQuery(sql1); System.out.print(\); System.out.println(); while(rs.next()){ System.out.print(\+rs.getInt(1)); System.out.print(\); System.out.print(\姓名=\+rs.getObject(2).toString()); System.out.print(\); System.out.print(\学号=\+rs.getObject(3).toString()); System.out.print(\); } } } 2、 三、 1、请实现一个接口dealDao的实现类dealDaoImpl 该接口定义如下: interface dealDao{ Boolean find(user user)//查找指定的用户是否存在 Boolean save(user user)//保存指定的用户信息 Int getSum(user user)//得到某人账户中的资金 Boolean change(user user,int sum)//给user转指定的钱 } User类{String name;//账户名 String pwd;//密码 int money;//金额 } 账户表(账户名char(10),密码 varchar(20),金额 int) import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; public class dealDaoImpl implements dealDao{ 实 验 过 程 、 结 果 及 分 析 Connection conn; PreparedStatement state=null; PreparedStatement state1=null; PreparedStatement state2=null; PreparedStatement state3=null; ResultSet rs=null; public dealDaoImpl(Connection conn){ } Boolean find(user user){ String sql=\用户表 where name=?\; String name=null; name=user.getName(); state=conn.prepareStatement(sql); state.setNString(1, name); rs= state.executeQuery(); if(rs.next()==true) this.conn=conn;

联系合同范文客服:xxxxx#qq.com(#替换为@)