Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2561914
  • 博文数量: 245
  • 博客积分: 4125
  • 博客等级: 上校
  • 技术积分: 3113
  • 用 户 组: 普通用户
  • 注册时间: 2009-03-25 23:56
文章分类

全部博文(245)

文章存档

2015年(2)

2014年(26)

2013年(41)

2012年(40)

2011年(134)

2010年(2)

分类: Java

2011-12-27 16:13:28

这个例子介绍怎样使用JDBC Statement进行批量Update。 Batch Update 不仅可以用于Insert,也可以用于Update和Delete语句。

代码如下:
  1.          conn = getDBConnection();
  2.          stmt = conn.createStatement();
  3.         
  4.          conn.setAutoCommit(false);
  5.         
  6.          stmt.addBatch(sql1);
  7.          stmt.addBatch(sql2);
  8.          stmt.addBatch(sql3);
  9.         
  10.          stmt.executeBatch();
  11.          conn.commit();

完整代码如下:

  1. package org.hnrsc.jdbc;

  2. import java.sql.Connection;
  3. import java.sql.DriverManager;
  4. import java.sql.ResultSet;
  5. import java.sql.SQLException;
  6. import java.sql.Statement;

  7. public class JDBCMySQL {

  8.     private static final String DB_DRIVER = "com.mysql.jdbc.Driver";
  9.     private static final String DB_CONNECTION = "jdbc:mysql://localhost:3306/test";
  10.     private static final String DB_USER = "root";
  11.     private static final String DB_PASSWORD = "";
  12.     
  13.     
  14.     public static void main(String[] args) {
  15.         try{
  16.             //createTable();
  17.             //insertRecord();
  18.             batchInsertRecords();
  19.             selectRecords();
  20.         }catch(SQLException e){
  21.             System.out.println(e.getMessage());
  22.         }
  23.     }
  24.     private static void insertRecord() throws SQLException{
  25.         Connection conn = null;
  26.         Statement stmt = null;
  27.         
  28.         String sql = "INSERT INTO t_user(username,password) values"
  29.             +"('Dendi','password')";
  30.         
  31.         try{
  32.             conn = getDBConnection();
  33.             stmt = conn.createStatement();
  34.             System.out.println(sql);
  35.             stmt.executeUpdate(sql);
  36.             System.out.println("A Record has been inserted!");
  37.         }catch(SQLException e){
  38.             System.out.println(e.getMessage());
  39.         }finally{
  40.             if(stmt!=null){
  41.                 stmt.close();
  42.             }
  43.             if(conn!=null){
  44.                 conn.close();
  45.             }
  46.         }
  47.     }
  48.     private static void createTable() throws SQLException{
  49.         Connection conn = null;
  50.         Statement stmt = null;
  51.         
  52.         String sql = "CREATE TABLE t_user("
  53.             +"id int auto_increment primary key,"
  54.             +"username varchar(50) NOT NULL,"
  55.             +"password varchar(50) NOT NULL"
  56.             +")";
  57.         
  58.         try{
  59.             conn = getDBConnection();
  60.             stmt = conn.createStatement();
  61.             System.out.println(sql);
  62.             stmt.execute(sql);
  63.             System.out.println("Table \"t_user\" is created!");
  64.         }catch(SQLException e){
  65.             System.out.println(e.getMessage());
  66.         }finally{
  67.             if(stmt!=null){
  68.                 stmt.close();
  69.             }
  70.             if(conn!=null){
  71.                 conn.close();
  72.             }
  73.         }
  74.         
  75.     }
  76.     
  77.     private static void selectRecords() throws SQLException{
  78.         Connection conn = null;
  79.         Statement stmt = null;
  80.         
  81.         String sql = "SELECT id,username,password FROM t_user";
  82.         try{
  83.             conn = getDBConnection();
  84.             stmt = conn.createStatement();
  85.             
  86.             System.out.println(sql);
  87.             
  88.             //execute select SQL statement
  89.             
  90.             ResultSet rs = stmt.executeQuery(sql);
  91.             while(rs.next()){
  92.                 int id = rs.getInt("id");
  93.                 String username = rs.getString("username");
  94.              System.out.println("id: "+ id);
  95.              System.out.println("username: "+ username);
  96.             }
  97.         }catch(SQLException e){
  98.             System.out.println(e.getMessage());
  99.         }finally{
  100.             if(stmt!=null){
  101.                 stmt.close();
  102.             }
  103.             if(conn!=null){
  104.                 conn.close();
  105.             }
  106.         }
  107.     }
  108.     private static void batchInsertRecords() throws SQLException{
  109.         Connection conn = null;
  110.         Statement stmt = null;
  111.         
  112.         String sql1 = "INSERT INTO t_user(username,password) values"
  113.             +"('Dendi','password')";
  114.         String sql2 = "INSERT INTO t_user(username,password) values"
  115.             +"('Kevin','pass111')";
  116.         String sql3 = "INSERT INTO t_user(username,password) values"
  117.             +"('Lily','mima')";
  118.         
  119.         try{
  120.             conn = getDBConnection();
  121.             stmt = conn.createStatement();
  122.         
  123.             conn.setAutoCommit(false);
  124.         
  125.          stmt.addBatch(sql1);
  126.          stmt.addBatch(sql2);
  127.          stmt.addBatch(sql3);
  128.         
  129.          stmt.executeBatch();
  130.          conn.commit();
  131.         
  132.             System.out.println("Batch Records have been inserted!");
  133.         }catch(SQLException e){
  134.             System.out.println(e.getMessage());
  135.         }finally{
  136.             if(stmt!=null){
  137.                 stmt.close();
  138.             }
  139.             if(conn!=null){
  140.                 conn.close();
  141.             }
  142.         }
  143.     }
  144.     private static Connection getDBConnection(){
  145.         Connection conn = null;
  146.         try{
  147.             Class.forName(DB_DRIVER);
  148.         }catch(ClassNotFoundException e){
  149.             System.out.println(e.getMessage());
  150.         }
  151.         try{
  152.             conn = DriverManager.getConnection(DB_CONNECTION,DB_USER, DB_PASSWORD);
  153.         }catch(SQLException e){
  154.             System.out.println(e.getMessage());
  155.         }
  156.         
  157.         return conn;
  158.     }

  159. }

阅读(2407) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~