2024年1月15日发(作者:)

XXX大 学 计 算 机 学 院 实 验 报 告

计算机学院 2017 级 软件工程 专业 5 班 指导教师 XX

学号 姓名 XXX 2019 年 11月 13 日 成绩

课程JavaWeb应用开发实验

1. 了解什么是jdbc

实验目的

2. 掌握jdbc操作数据库的步骤

3. 学会使用eclipse连接MySQL,并对数据库进行增删改查等操作

4. 了解和掌握PrepareStatement对象的相关属性及使用

5. 学会使用PrepareStatement对象批量处理数据

实验试验九:数据批处理操作

实验仪器

和器材

电脑、jdk、Tomcat、eclipse、DOS命令窗口、IE浏览器

试验:

:按照下面要求设计实现PreparedStatement对象的相关批处理操作

要求如下所示

1.新建数据库

2.指定要执行的sql语句如下:

String sql = “insert into users(name,password) value (?,?)”;

3.编写JDBCUtils工具类,类中要包含连接和释放资源的方法

4.编写Exception类,要求在类中使用JDBCUtils工具类获取连接和释放资源,并使用PreparedStatement对象批处理添加5条记录。

5.按照MVC模式搭建项目

6.批处理的执行参看PreparedStatement类的方法addBatch()、executeBatch()、clearBatch().

①建立数据库

②eclipse连接MySQL,并查询出表中所有的数据

核心代码如下所示:

//加载驱动,并建立数据库连接

public static Connection getConnection() throws

SQLException,ClassNotFoundException{

Connection conn=null;

Statement Stmt = null;

ResultSet rs = null;

e("");

String url = "jdbc:mysql://localhost:3306/mysql";

}

String username="root";

String passwords = "t709791G";

//创建应用程序与数据库连接的Connection对象

Connection conn = nection(url, username,

return conn;

passwords);

//通过Connection对象获取statement对象

Stmt = Statement();

//使用Statement执行sql语句

String sql1 = "select * from users";

rs = eQuery(sql1);//返回结果集

//操作ResultSet结果集

n("id name password");

while(()){

}

tackTrace();

//回收数据库资源

if(rs!=null){

}

if(Stmt != null){

}

if(conn!=null){

try {

}

Stmt = null;

();

tackTrace();

} catch (SQLException e) {

try {

}

rs = null;

();

tackTrace();

} catch (SQLException e) {

int id = ("id");//通过列明获取指定字段的值

String name = ing("name");

String password = ing("password");

n(id+" "+name+" "+password);

} catch (ClassNotFoundException e) {

}finally{

}}}

try {

}

conn = null;

();

tackTrace();

} catch (SQLException e) {

完成效果:

图 查询出数据表中所有的数据

③编写JDBCUtils工具类,类中要包含连接和释放资源的方法,插入语句:String sql

= “insert into users(name,password) value (?,?)”;

代码如下所示:

package ;

/**

*

* 封装对表的增加、删除、修改、查新操作

* */

public class UsersDao {

//添加用户操作

public static void insert (user users){

Connection conn = null;

Statement stmt = null;

ResultSet rs = null;

try {

//获取数据库连接

conn = nection();

//获取statement对象

stmt = Statement();

//String sql = "INSERT INTO users(id,NAME,PASSWORD)"+"

VALUES("+()+",'"+e()+"','"+sword()+"') ";

}

String sql = "insert into users(id,name,password) values(?,?,?)";

//创建指定sql语句的PreparedStatement对象

PreparedStatement preStm = eStatement(sql);

//PreparedStatement批量添加5条记录

(1, ());

ing(2, e());

ing(3, sword());

eUpdate();

ch();//将sql语句添加到缓存中

eBatch();//每5条发送一次数据

atch();//发送后清空缓存中的数据

} catch (ClassNotFoundException e) {

tackTrace();

} catch (SQLException e) {

tackTrace();

} finally{

}

e(rs, stmt, conn);

//查询所有的User对象

public ArrayList findAll(){

Connection conn = null;

Statement stmt = null;

ResultSet rs = null;

ArrayList list = new ArrayList();

try {

//获取数据库连接

conn = nection();

//获取statement对象

stmt = Statement();

//发送sql语句

String sql = "select * from users";

rs = eQuery(sql);

//处理结果集

while(()){

}

return list;

user users = new user();

(("id"));

e(ing("name"));

e(ing("password"));

(users);

} catch (ClassNotFoundException e) {

tackTrace();

} catch (SQLException e) {

tackTrace();

}finally{

}

return null;}

e(rs, stmt, conn);

//根据ID查找指定的user

public user find(int id){

//删除用户

public boolean delete(int id){

Connection conn = null;

Connection conn = null;

Statement stmt = null;

ResultSet rs = null;

try {

//获取数据库连接

conn = nection();

//获取statement对象

stmt = Statement();

//发送sql语句

String sql = "select * from users where id=" + id ;

rs = eQuery(sql);

//处理结果集

while(()){

}

return null;

user users = new user();

(("id"));

e(ing("name"));

e(ing("password"));

return users;

} catch (Exception e) {

// TODO: handle exception

}finally{

}

return null;}

e(rs, stmt, conn);

}

Statement stmt = null;

ResultSet rs = null;

try {

//获取数据库连接

conn = nection();

//获取statement对象

stmt = Statement();

//发送sql语句

String sql = "delete from users where id=" + id;

int num = eUpdate(sql);

if(num >0){//判断是否找到

}

return false;

return true;

} catch (Exception e) {

tackTrace();

}finally{

}

e(rs, stmt, conn);

return false;

//修改用户

public boolean update(user users){

Connection conn = null;

Statement stmt = null;

ResultSet rs = null;

try {

//获取数据库连接

conn = nection();

//获取statement对象

stmt = Statement();

//发送sql语句

String sql = "update users set

name='"+e()+"',password='"+sword()+"'where

id="+();

}

④使用PreparedStatement对象批处理添加5条记录。

package e;

import ao;

import ;

import ils;

/**

* 向user表中插入数据

* @author Administrator

*

}

int num = eUpdate(sql);

if(num>0){

}

return false;

} catch (ClassNotFoundException e) {

tackTrace();

return true;

} catch (SQLException e) {

tackTrace();

}finally{

}

e(rs, stmt, conn);

return false;

*/

public class jdbcInsert {

}

public static void main(String [] args){

}

JDBCUtils util = new JDBCUtils();

user users = new user();

for(int i=1;i<6;i++){

(i+3);

e("Lingling");

sword("1234");

(users);}

public static void insert (user users){

Connection conn = null;

Statement stmt = null;

ResultSet rs = null;

try {

}

//获取数据库连接

conn = nection();

//获取statement对象

stmt = Statement();

//创建指定sql语句的PreparedStatement对象

PreparedStatement preStm = eStatement(sql);

//PreparedStatement批量添加5条记录

(1, ());

ing(2, e());

ing(3, sword());

eUpdate();

ch();//将sql语句添加到缓存中

eBatch();//每5条发送一次数据

atch();//发送后清空缓存中的数据

String sql = "insert into users(id,name,password) values(?,?,?)";

} catch (ClassNotFoundException e) {

tackTrace();

tackTrace();

e(rs, stmt, conn);

}

} catch (SQLException e) {

} finally{

完成效果

图 向表中插入数据后,再查询后的结果

⑤按照MVC模式搭建项目

心得与体会:

Manager类用于加载JDBC驱动并创建与数据库的连接。

ton接口代表Java程序和数据库之间的连接,只有获得该连接对象才能访问数据库,并操作数据表。

ent接口用于执行静态的SQL语句,并返回一个结果集,该接口的对象通过Connection实例的CreateStatement()方法。

ent接口封装了JDBC执行SQL语句,可以完成Java程序执行SQL语句的操作。PreparedStatement是Statement的子接口,用于执行预编译的SQL语句,应用该接口中的SQL语句可以使用占位符“?”来代替参数,然后通过setXxx()方法为SQL语句的参数值。

Set接口用于保存JDBC执行查询时返回到的结果集,该结果集封装到一个逻辑表格中。ResultSet对象初始化时,游标在表格的第一行之前,调用next()方法作为while循环的条件来迭代ResultSet结果。

6.加载并注册数据库驱动:

erDriver(Driver driver);

或 e(“DriverName”);

7.获取连接数据库的具体连接方式:

Connection conn=

nection(String url,String

user,String pwd);

8. 可以使用PreparedStatement对象批量向数据库中添加数据,addBatch();将sql语句添加到缓存中executeBatch();每5条发送一次数据clearBatch();发送后清空缓存中的数据