数据库的批处理

import java.sql.Connection; 
    import java.sql.Date; 
    import java.sql.PreparedStatement; 
    import java.sql.ResultSet; 
    import java.sql.SQLException; 
    public class BatchTest { 
        public static void main(String[] args) throws SQLException { 
            createBatch(); 
        } 
        public static void createBatch()throws SQLException{ 
        String sql="insert into student(id,name,birthday,sal)values(?,?,?,?)"; 
        Connection conn=null; 
        PreparedStatement ps=null; 
        ResultSet rs=null; 
        try{ 
            conn=JdbcUtil.getConnection(); 
            ps=conn.prepareStatement(sql); 
            for(int i=0;i<100;i++)
            { 
                 ps.setInt(1, i); 
                 ps.setString(2, "name"+i); 
                 ps.setDate(3, new Date(System.currentTimeMillis())); 
                 ps.setInt(4, i); 
                 ps.addBatch(); 
            } 
            ps.executeBatch(); 
             
        }finally { 
            JdbcUtil.free(rs, ps, conn); 
        } 
        } 
    } 
   ps.addBatch()---相当于打包
   ps.executeBatch()---将包里面的数据一起处理

你可能感兴趣的:(jdbc,bach)