jdbc executeBatch no different
I'm using Microsoft jdbc driver sp2 with MS SQL Server 2000 sp3. I'm
trying to reduce the network calls to the db by using batch inserts.
Below is the sample code, standard and batch method. Batch is no
faster. A network ****yzer shows that either way the driver is doing
remote procedure calls for each insert. I've tried Data Directs
driver and it does batch properly and much faster. Can someone show
me what I am missing.
//STANDARD
String url = "jdbc:microsoft:sqlserver://myServer:1433User=me;Password=pass;DataBaseName=Ba tchTest";
String sql = "insert into Something( Something, TestSomething)
values(?, ? )";
Connection cn = null;
PreparedStatement ps = null;
try {
Class.forName("com.microsoft.jdbc.sqlserver.SQLSer verDriver");
cn = DriverManager.getConnection(url);
ps = cn.prepareStatement(sql);
int i = 0;
while( i++ < 1000 ) {
ps.setString(1, "This is something: " + i );
ps.setString(2, "Something more : " + i );
ps.execute();
}
} catch (Exception e) {
System.out.println("problem: " + e.getMessage() );
} finally {
try {ps.close(); } catch (Exception e) {}
try{ cn.close(); } catch (Exception e1) {}
}
//BATCH
try {
Class.forName("com.microsoft.jdbc.sqlserver.SQLSer verDriver");
cn = DriverManager.getConnection(url);
ps = cn.prepareStatement(sql);
int i = 0;
while( i++ < 1000 ) {
ps.setString(1, "This is something: " + i );
ps.setString(2, "Something more : " + i );
ps.addBatch();
}
int[] rows = ps.executeBatch();
} catch (Exception e) {
System.out.println("problem: " + e.getMessage() );
} finally {
try {ps.close(); } catch (Exception e) {}
try{ cn.close(); } catch (Exception e1) {}
}
Thanks,
Greg
|