jdbc批量插入、批量删除、批量更新
转载:http://blog.sina.com.cn/s/blog_662e56ec0100jtg5.html
?
一、JDBC的批量插入
???JDBC批量插入主要用于数据导入和日志记录因为日志一般都是先写在文件下的等。
??? 我用Mysql5.1.5的JDBC driver 分别对三种比较常用的方法做了测试
???方法一,使用PreparedStatement加批量的方法
String sql="delete from table where id in(0"; String id[]=要删除的ID数组; for(int i=0;i <id.length;i++) { sql+=","+id[i]; } sql+=")"; PreparedStatement ps=conn.prepareStatement(sql); ps.execute(); public void deleteBat(Integer[] catNo){ try { Connection con=DBUtil.getInstance().getCon(); String sql="delete from cat where catno=?"; con.setAutoCommit(false); PreparedStatement ps=con.prepareStatement(sql); for (Integer in : catNo) { ps.setInt(1, in); ps.addBatch(); } int[] result=ps.executeBatch(); con.commit(); for (int i : result) { System.out.println(i); } } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } ?