conn.setAutoCommit(false); Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY); for (int x = 0; x < size; x++) { stmt .execute("INSERT INTO adlogs(ip,website,yyyymmdd,hour,object_id) VALUES('192.168.1.3', 'localhost','20081009',8,'23123')"); } conn.commit();
JDBC批量插入主要用于数据导入和日志记录因为日志一般都是先写在文件下的等。
我用Mysql 5.1.5的JDBC driver 分别对三种比较常用的方法做了测试
try { Class.forName("com.mysql.jdbc.Driver"); conn = DriverManager.getConnection(o_url, userName, password); conn.setAutoCommit(false); String sql = "INSERT adlogs(ip,website,yyyymmdd,hour,object_id) VALUES(?,?,?,?,?)"; PreparedStatement prest = conn .prepareStatement(sql, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY); for (int x = 0; x < size; x++) { prest.setString(1, "192.168.1.1"); prest.setString(2, "localhost"); prest.setString(3, "20081009"); prest.setInt(4, 8); prest.setString(5, "11111111"); prest.addBatch(); } prest.executeBatch(); conn.commit(); conn.close(); } catch (SQLException ex) { Logger.getLogger(MyLogger.class.getName()).log(Level.SEVERE, null, ex); } catch (ClassNotFoundException ex) { Logger.getLogger(MyLogger.class.getName()).log(Level.SEVERE, null, ex); }
conn.setAutoCommit(false); Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY); for (int x = 0; x < size; x++) { stmt .addBatch("INSERT INTO adlogs(ip,website,yyyymmdd,hour,object_id) VALUES('192.168.1.3', 'localhost','20081009',8,'23123')"); } stmt.executeBatch(); conn.commit();
conn.setAutoCommit(false)
自身感受:
上面的方法是网上能长的3种比较常用的方法,但我在用上面方法的时候都报游标数不足的异常,
这跟oracle设置没有关系,因为oracle不可能因为这点原因而开到无限大。我是这样解决的:
用多线程分批插入,每批插入200-300个数据,这样一个线程上的效率上没有什么变化