当前位置:Gxlcms > 数据库问题 > JDBC操作数据库之批处理

JDBC操作数据库之批处理

时间:2021-07-01 10:21:17 帮助过:26人阅读

<%@ 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 }

程序运行结果:

技术分享

 

JDBC操作数据库之批处理

标签:时间   image   java   lan   cal   ges   on()   coding   state   

人气教程排行