Prentice Hall and Sun Microsystems. Personal use only; do not redistribute.
498
Chapter 18 JDBC and Database Connection Pooling
int[] employeeIDs = getIDs();
for(int i=0; i
statement.setFloat(1, newSalaries[i]);
statement.setInt(2, employeeIDs[i]);
statement.execute();
}
The performance advantages of prepared statements can vary significantly,
depending on how well the server supports precompiled queries and how effi
ciently the driver handles raw queries. For example, Listing 18.18 presents a
class that sends 40 different queries to a database using prepared statements,
then repeats the same 40 queries using regular statements. With a PC and a
28.8K modem connection to the Internet to talk to an Oracle database, pre
pared statements took only half the time of raw queries, averaging 17.5 seconds
for the 40 queries as compared with an average of 35 seconds for the raw que
ries. Using a fast LAN connection to the same Oracle database, prepared state
ments took only about 70 percent of the time required by raw queries,
averaging 0.22 seconds for the 40 queries as compared with an average of 0.31
seconds for the regular statements. With Sybase, prepared statement times
were virtually identical to times for raw queries both with the modem connec
tion and with the fast LAN connection. To get performance numbers for your
setup, download
DriverUtilities
.java from
http://www.coreserv
lets.com/
, add information about your drivers to it, then run the
Prepared
Statements
program yourself.
Listing 18.18 PreparedStatements.java
package coreservlets;
import java.sql.*;
/** An example to test the timing differences resulting
* from repeated raw queries vs. repeated calls to
* prepared statements. These results will vary dramatically
* among database servers and drivers.
*/
public class PreparedStatements {
public static void main(String[] args) {
if (args.length < 5) {
printUsage();
return;
}
String vendorName = args[4];
int vendor = DriverUtilities.getVendor(vendorName);
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