在jdbc2.0里添加了批量处理的功能(batch),其同意将多个sql语句作为一个单元送至数据库去运行,这样做能够提高操作效率。在操作大量的数据时, ORM框架实现批量是非常慢的。我们能够使用jdbc提供的Batch来提高效率。
演示样例:
首先是使用for循环,一句一句的运行:
public class TestCommon {
	static long startTime;
	public static void main(String[] args) throws Exception {  
		
        Connection conn = getConnection();  
        PreparedStatement ps = null;  
        try {  
        	startTime=System.nanoTime();   //获取開始时间 
            ps = conn  
                    .prepareStatement("INSERT INTO batchtab  values (?, ?)");    
            conn.setAutoCommit(false);              
            for (int n = 0; n < 10000; n++) {  
                Integer i = new Integer(n);  
                ps.setString(1, i.toString());  
                ps.setString(2, "value" + i.toString());  
                ps.executeUpdate();  
            }  
            conn.commit();
            long endTime=System.nanoTime(); //获取结束时间
            System.out.println("程序执行时间: "+(endTime-startTime)+"ns"); 
        }catch (SQLException ex) {  
            System.out.println("SQLException: " + ex.getMessage());  
            System.out.println("SQLState: " + ex.getSQLState());  
            System.out.println("Message: " + ex.getMessage());  
            System.out.println("Vendor error code: " + ex.getErrorCode());  
        } catch (Exception e) {  
            e.printStackTrace();  
            System.err.println("Exception: " + e.getMessage());  
        } finally {  
            if (conn != null)  
                conn.close();  
            if (ps != null)  
                ps.close();        
        }  
    }  
  
    
    public static Connection getConnection() {  
    	  Connection con = null;  //创建用于连接数据库的Connection对象  
          try {  
              Class.forName("com.mysql.jdbc.Driver");// 载入Mysql数据驱动  
                
              con = DriverManager.getConnection(  
                      "jdbc:mysql://localhost:3306/TestBatch", "root", "123456");// 创建数据连接  
                
          } catch (Exception e) {  
              System.out.println("数据库连接失败" + e.getMessage());  
          }  
          return con; //返回所建立的数据库连接  
    }  
}  
使用Batch,批量操作:
public class TestPreStatementBatch {
	static long startTime;
	public static void main(String[] args) throws Exception {  
		
        Connection conn = getConnection();  
        ResultSet rs = null;  
        PreparedStatement ps=null;  
        try {  
  
        	 startTime=System.nanoTime();   //获取開始时间  
             
            ps = conn.prepareStatement("INSERT INTO batchtab values (?, ?
)");  
            conn.setAutoCommit(false);  
            ps.clearBatch();  
 
            for (int n=0; n<10000; n++) { 
            	Integer i = new Integer(n); 
                ps.setString(1, i.toString());  
                ps.setString(2, "value" + i.toString());  
                ps.addBatch();  
            }  
            ps.executeBatch();  
            conn.commit();  
            long endTime=System.nanoTime(); //获取结束时间
            //打印消耗时间
            System.out.println("程序执行时间: "+(endTime-startTime)+"ns");  
        } catch (BatchUpdateException b) {  
            System.out.println("SQLException: " + b.getMessage());  
            System.out.println("SQLState: " + b.getSQLState());  
            System.out.println("Message: " + b.getMessage());  
            System.out.println("Vendor error code: " + b.getErrorCode());  
            System.out.print("Update counts: ");  
           
        } catch (SQLException ex) {  
            System.out.println("SQLException: " + ex.getMessage());  
            System.out.println("SQLState: " + ex.getSQLState());  
            System.out.println("Message: " + ex.getMessage());  
            System.out.println("Vendor error code: " + ex.getErrorCode());  
        } catch (Exception e) {  
            e.printStackTrace();  
            System.err.println("Exception: " + e.getMessage());  
        } finally {  
            if( conn != null )  
            conn.close();  
         if(ps !=null)  
            ps.close();  
            if(rs !=null)  
            rs.close();   
            
        }  
    }  
  
	public static Connection getConnection() {  
  	  Connection con = null;  //创建用于连接数据库的Connection对象  
        try {  
            Class.forName("com.mysql.jdbc.Driver");// 载入Mysql数据驱动  
              
            con = DriverManager.getConnection(  
                    "jdbc:mysql://localhost:3306/TestBatch", "root", "123456");// 创建数据连接  
              
        } catch (Exception e) {  
            System.out.println("数据库连接失败" + e.getMessage());  
        }  
        return con; //返回所建立的数据库连接  
  }  	
}
    
一条条的循环插入是每插入一条数据都会调用一次运行;而Batch是把全部的数据全都存起来。之后调用一次运行。数据量非常大的话,效率就会差非常多。
通过插入一万条一样的数据消耗的时间,我们能够看到相差的时间。我们能够通过降低语句的多次运行来提高性能。事实上,同.NET中的SqlBulkCopy思想一样。一次运行WriteToServer。就如同生活中我们做事情一样。不能仅仅想做了即可,还要多多思考有没有什么方法能够做到更好。
原文:http://www.cnblogs.com/zhchoutai/p/6729867.html