利用JDBC对数据库进行批量插入数据操作,执行次数100万

版权:JavaIT学习室
转载请标明,http://www.javait.org

在学习Java的JDBC技术,如果想利用JDBC对数据库进行批量插入数据操作,执行次数100万。我们应该如何实现?我们还是直接将代码给大家呈现出来

  1 package com.gxa.edu;
  2  
  3 import javax.swing.JFrame;
  4 import javax.swing.JButton;
  5 import javax.swing.ImageIcon;
  6 import javax.swing.JLabel;
  7 import javax.swing.JTextArea;
  8 import javax.swing.JTextField;
  9 import javax.swing.JPasswordField;
 10 import javax.swing.JComboBox;
 11 import javax.swing.JRadioButton;
 12 import javax.swing.ButtonGroup;
 13  
 14 import java.awt.FlowLayout;
 15 import java.awt.Color;
 16 import java.sql.Connection;
 17 import java.sql.DriverManager;
 18 import java.sql.ResultSet;
 19 import java.sql.SQLException;
 20 import java.sql.Statement;
 21  
 22 /**
 23  * @author Administrator
 24  * 国信安百杰高端Java培训
 25  */
 26 public class Test extends JFrame {
 27     public final String url = "jdbc:sqlserver://127.0.0.1:1433;DatabaseName=stu";
 28     public final String username = "sa";
 29     public final String password = "123456";
 30     public final String classdriver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
 31  
 32     public Connection conn;
 33     public Statement stmt;
 34     public ResultSet rs;
 35  
 36     public Test() {
 37         try {
 38             Class.forName(classdriver);
 39         } catch (ClassNotFoundException e) {
 40             // TODO Auto-generated catch block
 41             e.printStackTrace();
 42         }
 43     }
 44  
 45     /**
 46      * 利用JDBC来创建表
 47      */
 48     public void createTable() {
 49         String sql = "create table batch (a1 varchar(10),a2 varchar(500))";
 50         try {
 51             conn = DriverManager.getConnection(url, username, password);
 52             stmt = conn.createStatement();
 53             boolean f = stmt.execute(sql);
 54             System.out.println(f);
 55         } catch (SQLException e) {
 56             e.printStackTrace();
 57         }
 58     }
 59  
 60     /**
 61      * 对数据库进行批量插入数据操作
 62      * 执行次数100万
 63      */
 64     public void insertBatch() {
 65         //思路:将100万条数据分成n等份,1等份为1000条数据
 66         //如何实现?
 67         //1、必须将Connection接口的自动提交方式改为手动
 68         //2、利用Statement接口中的如下三个方法:addBatch、clearBath、executeBatch
 69         try {
 70             conn = DriverManager.getConnection(url, username, password);
 71             conn.setAutoCommit(false);
 72             stmt = conn.createStatement();
 73             for (int i = 0; i < 1000000; i++) {
 74                 String sql = "insert into batch values ('"+i+"', '第"+i+"条数据')";
 75                 //利用addBatch方法将SQL语句加入到stmt对象中
 76                 stmt.addBatch(sql);
 77                 if (i % 1000 == 0 && i != 0) {
 78                     //利用executeBatch方法执行1000条SQL语句
 79                     stmt.executeBatch();
 80                     stmt.clearBatch();
 81                     conn.commit();
 82                 }
 83             }
 84             stmt.executeBatch();
 85             stmt.clearBatch();
 86             conn.commit();
 87             close(); //关闭资源
 88         } catch (SQLException e) {
 89             e.printStackTrace();
 90         }
 91     }
 92  
 93     public void close() {
 94         try {
 95             if (rs != null) rs.close();
 96             if (stmt != null) stmt.close();
 97             if (conn != null) conn.close();
 98         } catch (SQLException e) {
 99             e.printStackTrace();
100         }
101     }
102  
103     /**
104      * @param args
105      */
106     public static void main(String[] args) {
107         // TODO Auto-generated method stub
108         Test t = new Test();
109         //t.createTable();
110         t.insertBatch();
111     }
112  
113 }
View Code

 

posted on 2013-09-24 18:41  JimSow  阅读(518)  评论(0)    收藏  举报

导航