Recently done projects need to insert large amounts of data into the database, before having the impression of Preparedstatement.executebatch (), so that you can reduce the interaction with the database; today, online search for how to achieve, Finding out more than just using Addbatch () and ExecuteBatch (), you also need to Conn.setautocommit (false) to turn off automatic transaction submission, or to reduce interaction. Also note that Addbatch () to a certain number of times is to be submitted in time, otherwise prone to memory overflow problem.
try {string url = "Jdbc:oracle:thin: @IP: 1521:ORCL";//ORCL is sid string user = "Oracle" for the database;
String Password = "Oracle";
StringBuffer sql = new StringBuffer ();
Sql.append ("INSERT into Ex_log (ex_log_id,ex_log_date) VALUES (?,?)");
Class.forName ("Oracle.jdbc.driver.OracleDriver");
Connection con = (Connection) drivermanager.getconnection (Url,user,password);
Close Transaction autocommit Con.setautocommit (false);
Final int batchsize = 10000;
int count = 0;
Long starttime = System.currenttimemillis ();
PreparedStatement PST = (preparedstatement) con.preparestatement (sql.tostring ());
for (int i = 0; i < list.size (); i++) {Exlog Exlog = (exlog) list.get (i);
Pst.setstring (1, Exlog.getexlogid ());
Pst.setstring (2, Exlog.getexlogdate ());
Add an SQL command to the command list Pst.addbatch ();
if (++count% batchsize = = 0) {pst.executebatch ();
Count = 0;
}///Perform batch update pst.executebatch ();
Execution of the statement, submit the transaction con.commit (); Long Endtime= System.currenttimemillis ();
System.out.println ("spents:" + (Endtime-starttime));
Pst.close ();
Con.close ();
catch (ClassNotFoundException e) {e.printstacktrace ();
catch (SQLException e) {e.printstacktrace (); }