Prentice Hall and Sun Microsystems. Personal use only; do not redistribute.
500
Chapter 18 JDBC and Database Connection Pooling
Listing 18.18 PreparedStatements.java (continued)
public static void doRawQueries(Connection conn,
boolean print) {
try {
String queryFormat =
"SELECT lastname FROM employees WHERE salary > ";
Statement statement = conn.createStatement();
long startTime = System.currentTimeMillis();
for(int i=0; i<40; i++) {
ResultSet results =
statement.executeQuery(queryFormat + (i*5000));
if (print) {
showResults(results);
}
}
long stopTime = System.currentTimeMillis();
double elapsedTime = (stopTime startTime)/1000.0;
System.out.println("Executing raw query " +
"40 times took " +
elapsedTime + " seconds.");
} catch(SQLException sqle) {
System.out.println("Error executing query: " + sqle);
}
}
private static void showResults(ResultSet results)
throws SQLException {
while(results.next()) {
System.out.print(results.getString(1) + " ");
}
System.out.println();
}
private static Connection getConnection(String driver,
String url,
String username,
String password) {
try {
Class.forName(driver);
Connection connection =
DriverManager.getConnection(url, username, password);
return(connection);
} catch(ClassNotFoundException cnfe) {
System.err.println("Error loading driver: " + cnfe);
return(null);
} catch(SQLException sqle) {
System.err.println("Error connecting: " + sqle);
return(null);
}
}
Second edition of this book: www.coreservlets.com; Sequel: www.moreservlets.com.
Servlet and JSP training courses by book's author: courses.coreservlets.com.
footer
Our partners:
PHP: Hypertext Preprocessor Best Web Hosting
Java Web Hosting
Jsp Web Hosting
Cheapest Web Hosting
Visionwebhosting.net Business web hosting division of Web
Design Plus. All rights reserved