Mega Code Archive

 
Categories / Java / Database SQL JDBC
 

JDBC Performance

/* MySQL and Java Developer's Guide Mark Matthews, Jim Cole, Joseph D. Gradecki Publisher Wiley, Published February 2003,  ISBN 0471269239 */ import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.Date; public class Performance {   Connection connection;   public Performance() {     try {       Class.forName("com.mysql.jdbc.Driver").newInstance();     } catch (Exception e) {       System.err.println("unable to load driver");     }     try {       connection = DriverManager           .getConnection("jdbc:mysql://192.168.1.81/archives?user=archives&password=archives");     } catch (SQLException e) {       System.out.println("SQLException: " + e.getMessage());       System.out.println("SQLState: " + e.getSQLState());       System.out.println("VendorError:  " + e.getErrorCode());     }   }   public void run() {     long startTime;     try {       /*        * PreparedStatement ps = connection.prepareStatement("INSERT INTO        * product VALUES(null, 'title', 5.54, 'supplier', null, ?)");        * startTime = new Date().getTime(); for (int i=0;i <1000;i++) {        * ps.setInt(1, i); ps.executeUpdate(); } System.out.println("INSERT = " +        * ((new Date().getTime()) - startTime));        */       Statement statement = connection.createStatement();       startTime = new Date().getTime();       for (int i = 0; i < 60; i++) {         ResultSet rs = statement             .executeQuery("SELECT pic_id, length, tlength, ts FROM them limit 100, "                 + (i * 1000));         rs.close();       }       //      ResultSet rs = statement.executeQuery("SELECT pic_id, length,       // tlength, ts FROM them");       //      rs.close();       statement.close();       System.out.println("SELECT = "           + ((new Date().getTime()) - startTime));       /*        * ps = connection.prepareStatement("UPDATE product SET inventory=10        * WHERE inventory = ?"); startTime = new Date().getTime(); for (int        * i=0;i <1000;i++) { ps.setInt(1, i); ps.executeUpdate(); }        * System.out.println("UPDATE = " + ((new Date().getTime()) -        * startTime));        */       connection.close();     } catch (SQLException e) {     }   }   public static void main(String[] args) {     Performance test = new Performance();     test.run();   } }