Java JDBC display first 500 records at a time, commit, than display the next 500 records and etc -
so want able display 500 records @ time, commit , print has been displayed records 1 500 records have been committed. , next 500 records , commit again until reached maximum records on 20k records. managed first 500 records stuck how can commit them , in commit them , continue next 500 records , on.
public static void selectrecordsicore() throws sqlexception { connection dbconnection = null; preparedstatement preparedstatement = null; statement statement = null; string selecttablesql = "select profile_id, ingress_flag, egress_flag, ce_ingress_flag, ce_egress_flag cos_profile" + " profile_id >= ? , profile_id <= ?;"; try { dbconnection = getinformixconnection(); //connects icore database system.out.println("i in try"); //gets max profile_id record statement = dbconnection.createstatement(); resultset r = statement.executequery("select max(profile_id) rowcount cos_profile"); r.next(); int maxcount = r.getint("rowcount"); system.out.println("cos_profile table before update has " + maxcount + " row(s)."); preparedstatement = dbconnection.preparestatement(selecttablesql); preparedstatement.setint(1, 1); preparedstatement.setint(2, maxcount); // execute select sql statement rs = preparedstatement.executequery(); updaterecordintobids(); } catch (sqlexception e) { system.out.println(e.getmessage()); } { if (rs != null) { rs.close(); } if (statement != null) { statement.close(); } if (preparedstatement != null) { preparedstatement.close(); } if (dbconnection != null) { dbconnection.close(); system.out.println("database icore connection closed"); } } } private static void updaterecordintobids() throws sqlexception { system.out.println("i inside update method"); connection dbconnection = null; preparedstatement preparedstatement = null; dbconnection = getoracleconnection(); //connects bids database string updatetablesql = "update traffic_profile_temp set pe_ingress_flag = ?, " + " pe_egress_flag = ?," + " ce_ingress_flag = ?," + " ce_egress_flag = ? " + " traffic_profile_id = ? "; preparedstatement = dbconnection.preparestatement(updatetablesql); try { int rowcount = 0; while (rs.next() && rowcount < 500) { // system.out.println("inside while loop"); string ingressflag = rs.getstring("ingress_flag"); //bids column pe_ingress_flag string egressflag = rs.getstring("egress_flag"); //bids column pe_egress_flag string ceingressflag = rs.getstring("ce_ingress_flag"); //bids column ce_ingress_flag string ceegressflag = rs.getstring("ce_egress_flag"); //bids column ce_egress_flag int profileid = rs.getint("profile_id"); //bids column traffic_profile_id preparedstatement.setstring(1, ingressflag); preparedstatement.setstring(2, egressflag); preparedstatement.setstring(3, ceingressflag); preparedstatement.setstring(4, ceegressflag); preparedstatement.setint(5, profileid); // system.out.println(updatetablesql); system.out.println("record " +profileid +" updated traffic_profile_temp table!"); // execute update sql stetement preparedstatement.addbatch(); rowcount++; system.out.println(rowcount); } preparedstatement.executebatch(); } catch (sqlexception e) { system.out.println(e.getmessage()); } { if (preparedstatement != null) { preparedstatement.close(); } if (dbconnection != null) { dbconnection.close(); system.out.println("database bids connection closed"); } } }
update part
while (rs.next() && rowcount < 500) {
with
while (rs.next()) {
and
// execute update sql stetement preparedstatement.addbatch(); rowcount++; system.out.println(rowcount);
with
// execute update sql stetement preparedstatement.addbatch(); rowcount++; if(rowcount % 500 == 0){ preparedstatement.executebatch(); } system.out.println(rowcount);
this check if rowcount can divided 500, execute batch. don't forget execute batch after statements finish execute remaining batches couldn't divided 500 . more details regarding batches
Comments
Post a Comment