JDBC操作数据库之批处理

JDBC开发中,操作数据库需要和数据库建立连接,然后将要执行的SQL语句发送到数据库服务器,最后关闭数据库连接,都是按照这样的操做的,如果按照此流程要执行多条SQL语句,那么就要建立多个数据库连接,将时间浪费在数据库连接上,针对这样的问题,JDBC给出了一个很好的解决方案------JDBC的批处理。

示例代码

(1)index.jsp

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 7 <title>批处理操作</title>
 8 </head>
 9 <body>
10     <jsp:useBean id="batch" class="com.java.Batch"></jsp:useBean>
11     <%
12         //执行批量插入操作 
13         int row = batch.saveBatch();
14         out.print("批量插入了【"+row+"条信息】");
15     %>
16 </body>
17 </html>
查看代码

(2)Batch类

 1 package com.java;
 2 
 3 import java.sql.Connection;
 4 import java.sql.DriverManager;
 5 import java.sql.PreparedStatement;
 6 import java.sql.SQLException;
 7 import java.util.Random;
 8 
 9 public class Batch {
10     public Connection getConnection() {
11         //数据库连接
12         Connection conn = null;
13         try {
14             //加载数据库驱动,注册到驱动管理器中
15             Class.forName("com.mysql.jdbc.Driver");
16             //数据库连接字符串
17             String url = "jdbc:mysql://localhost:3306/test";
18             //数据库用户名
19             String username = "root";
20             //数据库密码
21             String password = "123456";
22             //创建Connection连接
23             conn = DriverManager.getConnection(url,username,password);
24             
25         }catch(ClassNotFoundException e) {
26             e.printStackTrace();
27         }catch(SQLException e) {
28         
29             e.printStackTrace();
30         }
31         //返回数据库连接
32         return conn;
33     }
34     /**
35      * 批量添加数据
36      * @return  所影响的行数
37      */
38     public int saveBatch() {
39         //行数
40         int row = 0;
41         //获取数据库连接
42         Connection conn = getConnection();
43         try {
44             //插入数据的SQL语句
45             String sql = "insert into book(id,name,price,bookCount,author) values(?,?,?,?,?)";
46             //创建PrepareStatement
47             PreparedStatement ps = conn.prepareStatement(sql);
48             //实例化Random
49             Random random = new Random();
50             //循环添加数据
51             for(int i=10;i<20;i++) {
52                 //对SQL语句中的第一个参数赋值
53                 ps.setInt(1, i+1);
54                 //对SQL语句中的第二个参数赋值
55                 ps.setString(2, "图书"+i);
56                 //对SQL语句中的第三个参数赋值
57                 ps.setDouble(3, i%2);
58                 //对SQL语句中的第四个参数赋值
59                 ps.setInt(4, random.nextInt(5)+10);
60                 //对SQL语句中的第五个参数赋值
61                 ps.setString(5, "作者"+i);
62                 //添加批处理命令
63                 ps.addBatch();
64             }
65             //执行批处理操作并返回计数组成的数组
66             int[] rows = ps.executeBatch();
67             //对行数赋值
68             row = rows.length;
69             //关闭PrepareStatement
70             ps.close();
71             //关闭Connection
72             conn.close();
73         }catch(Exception e) {
74             e.printStackTrace();
75         }
76         
77         return row;
78     }
79 }

程序运行结果:

原文地址:https://www.cnblogs.com/lihuibin/p/7490122.html