实验内容数据批处理操作.docx

上传人:b****1 文档编号:2294856 上传时间:2023-05-03 格式:DOCX 页数:16 大小:258.39KB
下载 相关 举报
实验内容数据批处理操作.docx_第1页
第1页 / 共16页
实验内容数据批处理操作.docx_第2页
第2页 / 共16页
实验内容数据批处理操作.docx_第3页
第3页 / 共16页
实验内容数据批处理操作.docx_第4页
第4页 / 共16页
实验内容数据批处理操作.docx_第5页
第5页 / 共16页
实验内容数据批处理操作.docx_第6页
第6页 / 共16页
实验内容数据批处理操作.docx_第7页
第7页 / 共16页
实验内容数据批处理操作.docx_第8页
第8页 / 共16页
实验内容数据批处理操作.docx_第9页
第9页 / 共16页
实验内容数据批处理操作.docx_第10页
第10页 / 共16页
实验内容数据批处理操作.docx_第11页
第11页 / 共16页
实验内容数据批处理操作.docx_第12页
第12页 / 共16页
实验内容数据批处理操作.docx_第13页
第13页 / 共16页
实验内容数据批处理操作.docx_第14页
第14页 / 共16页
实验内容数据批处理操作.docx_第15页
第15页 / 共16页
实验内容数据批处理操作.docx_第16页
第16页 / 共16页
亲,该文档总共16页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

实验内容数据批处理操作.docx

《实验内容数据批处理操作.docx》由会员分享,可在线阅读,更多相关《实验内容数据批处理操作.docx(16页珍藏版)》请在冰点文库上搜索。

实验内容数据批处理操作.docx

实验内容数据批处理操作

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

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

学号 姓名XXX2019年11月13日成绩

课程名称

JavaWeb应用开发实验

实验名称

试验九:

数据批处理操作

实验目的

1.了解什么是jdbc

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

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

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

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

实验仪器

和器材

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

 

 

 

 

 

试验:

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

要求如下所示

1.新建数据库

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

Stringsql=“insertintousers(name,password)value(?

?

)”;

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

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

5.按照MVC模式搭建项目

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

①建立数据库

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

核心代码如下所示:

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

publicstaticConnectiongetConnection()throwsSQLException,ClassNotFoundException{

Connectionconn=null;

StatementStmt=null;

ResultSetrs=null;

Class.forName("com.mysql.jdbc.Driver");

Stringurl="jdbc:

mysql:

//localhost:

3306/mysql";

Stringusername="root";

Stringpasswords="t709791G";

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

Connectionconn=DriverManager.getConnection(url,username,passwords);

returnconn;

}

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

Stmt=conn.createStatement();

//使用Statement执行sql语句

Stringsql1="select*fromusers";

rs=Stmt.executeQuery(sql1);//返回结果集

//操作ResultSet结果集

System.out.println("idnamepassword");

while(rs.next()){

intid=rs.getInt("id");//通过列明获取指定字段的值

Stringname=rs.getString("name");

Stringpassword=rs.getString("password");

System.out.println(id+""+name+""+password);

}

}catch(ClassNotFoundExceptione){

e.printStackTrace();

}finally{

//回收数据库资源

if(rs!

=null){

try{

rs.close();

}catch(SQLExceptione){

e.printStackTrace();

}

rs=null;

}

if(Stmt!

=null){

try{

Stmt.close();

}catch(SQLExceptione){

e.printStackTrace();

}

Stmt=null;

}

if(conn!

=null){

try{

conn.close();

}catch(SQLExceptione){

e.printStackTrace();

}

conn=null;

}}}

完成效果:

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

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

Stringsql=“insertintousers(name,password)value(?

?

)”;

代码如下所示:

packagecn.itcast.dao;

/**

*

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

**/

publicclassUsersDao{

//添加用户操作

publicstaticvoidinsert(userusers){

Connectionconn=null;

Statementstmt=null;

ResultSetrs=null;

try{

//获取数据库连接

conn=JDBCUtils.getConnection();

//获取statement对象

stmt=conn.createStatement();

//Stringsql="INSERTINTOusers(id,NAME,PASSWORD)"+"VALUES("+user.getId()+",'"+user.getName()+"','"+user.getPassword()+"')";

Stringsql="insertintousers(id,name,password)values(?

?

?

)";

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

PreparedStatementpreStm=conn.prepareStatement(sql);

//PreparedStatement批量添加5条记录

preStm.setInt(1,users.getId());

preStm.setString(2,users.getName());

preStm.setString(3,users.getPassword());

preStm.executeUpdate();

preStm.addBatch();//将sql语句添加到缓存中

preStm.executeBatch();//每5条发送一次数据

preStm.clearBatch();//发送后清空缓存中的数据

}catch(ClassNotFoundExceptione){

e.printStackTrace();

}catch(SQLExceptione){

e.printStackTrace();

}finally{

JDBCUtils.release(rs,stmt,conn);

}

}

//查询所有的User对象

publicArrayListfindAll(){

Connectionconn=null;

Statementstmt=null;

ResultSetrs=null;

ArrayListlist=newArrayList();

try{

//获取数据库连接

conn=JDBCUtils.getConnection();

//获取statement对象

stmt=conn.createStatement();

//发送sql语句

Stringsql="select*fromusers";

rs=stmt.executeQuery(sql);

//处理结果集

while(rs.next()){

userusers=newuser();

users.setId(rs.getInt("id"));

users.setName(rs.getString("name"));

users.setName(rs.getString("password"));

list.add(users);

}

returnlist;

}catch(ClassNotFoundExceptione){

e.printStackTrace();

}catch(SQLExceptione){

e.printStackTrace();

}finally{

JDBCUtils.release(rs,stmt,conn);

}

returnnull;}

//根据ID查找指定的user

publicuserfind(intid){

Connectionconn=null;

Statementstmt=null;

ResultSetrs=null;

try{

//获取数据库连接

conn=JDBCUtils.getConnection();

//获取statement对象

stmt=conn.createStatement();

//发送sql语句

Stringsql="select*fromuserswhereid="+id;

rs=stmt.executeQuery(sql);

//处理结果集

while(rs.next()){

userusers=newuser();

users.setId(rs.getInt("id"));

users.setName(rs.getString("name"));

users.setName(rs.getString("password"));

returnusers;

}

returnnull;

}catch(Exceptione){

//TODO:

handleexception

}finally{

JDBCUtils.release(rs,stmt,conn);

}

returnnull;}

//删除用户

publicbooleandelete(intid){

Connectionconn=null;

Statementstmt=null;

ResultSetrs=null;

try{

//获取数据库连接

conn=JDBCUtils.getConnection();

//获取statement对象

stmt=conn.createStatement();

//发送sql语句

Stringsql="deletefromuserswhereid="+id;

intnum=stmt.executeUpdate(sql);

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

returntrue;

}

returnfalse;

}catch(Exceptione){

e.printStackTrace();

}finally{

JDBCUtils.release(rs,stmt,conn);

}

returnfalse;

}

//修改用户

publicbooleanupdate(userusers){

Connectionconn=null;

Statementstmt=null;

ResultSetrs=null;

try{

//获取数据库连接

conn=JDBCUtils.getConnection();

//获取statement对象

stmt=conn.createStatement();

//发送sql语句

Stringsql="updateuserssetname='"+users.getName()+"',password='"+users.getPassword()+"'whereid="+users.getId();

intnum=stmt.executeUpdate(sql);

if(num>0){

returntrue;

}

returnfalse;

}catch(ClassNotFoundExceptione){

e.printStackTrace();

}catch(SQLExceptione){

e.printStackTrace();

}finally{

JDBCUtils.release(rs,stmt,conn);

}

returnfalse;

}

}

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

jdbcInsert.Java

packagecn.itcast.example;

importcn.itcast.dao.UsersDao;

importcn.itcast.domain.user;

importcn.itcast.util.JDBCUtils;

/**

*向user表中插入数据

*@authorAdministrator

*

*/

publicclassjdbcInsert{

publicstaticvoidmain(String[]args){

JDBCUtilsutil=newJDBCUtils();

userusers=newuser();

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

users.setId(i+3);

users.setName("Lingling");

users.setPassword("1234");

UsersDao.insert(users);}

}

}

userDao.java

publicstaticvoidinsert(userusers){

Connectionconn=null;

Statementstmt=null;

ResultSetrs=null;

try{

//获取数据库连接

conn=JDBCUtils.getConnection();

//获取statement对象

stmt=conn.createStatement();

Stringsql="insertintousers(id,name,password)values(?

?

?

)";

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

PreparedStatementpreStm=conn.prepareStatement(sql);

//PreparedStatement批量添加5条记录

preStm.setInt(1,users.getId());

preStm.setString(2,users.getName());

preStm.setString(3,users.getPassword());

preStm.executeUpdate();

preStm.addBatch();//将sql语句添加到缓存中

preStm.executeBatch();//每5条发送一次数据

preStm.clearBatch();//发送后清空缓存中的数据

}catch(ClassNotFoundExceptione){

e.printStackTrace();

}catch(SQLExceptione){

e.printStackTrace();

}finally{

JDBCUtils.release(rs,stmt,conn);

}}

完成效果

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

⑤按照MVC模式搭建项目

 

心得与体会:

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

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

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

4.Statement接口封装了JDBC执行SQL语句,可以完成Java程序执行SQL语句的操作。

PreparedStatement是Statement的子接口,用于执行预编译的SQL语句,应用该接口中的SQL语句可以使用占位符“?

”来代替参数,然后通过setXxx()方法为SQL语句的参数值。

5.ResultSet接口用于保存JDBC执行查询时返回到的结果集,该结果集封装到一个逻辑表格中。

ResultSet对象初始化时,游标在表格的第一行之前,调用next()方法作为while循环的条件来迭代ResultSet结果。

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

DriverManager.registerDriver(Driverdriver);

或class.forName(“DriverName”);

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

Connectionconn=DriverManager.getConnection(Stringurl,Stringuser,Stringpwd);

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

 

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 表格模板 > 合同协议

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2