iming differences that occur between the execution of repeated raw queries

The following code tests the timing differences that occur between the execution of repeated raw queries and repeated calls to prepared statements:

public class PreparedStatements

{

public static void main(String[] args)

{

if (args.length < 5)

{

printUsage();

return;

}

String vendor = args[4];

DriverUtilities.loadDrivers();

if (!DriverUtilities.isValidVendor(vendor))

{

printUsage();

return;

}

String driver = DriverUtilities.getDriver(vendor);

String host = args[0];

String dbName = args[1];

String url = DriverUtilities.makeURL(host, dbName, vendor);

String username = args[2];

String password = args[3];

boolean print = false;

if ((args.length > 5) && (args[5].equals("print")))

{

print = true;

}

Connection connection =

ConnectionInfoBean.getConnection(driver, url, username, password);

if (connection != null)

{

doPreparedStatements(connection, print);

doRawQueries(connection, print);

}

try

{

connection.close();

}

catch(SQLException sqle)

{

System.err.println("Problem closing connection: " + sqle);

}

}

private static void doPreparedStatements(Connection conn, boolean print)

{

try

{

String queryFormat =

"SELECT id FROM music WHERE price < ?";

PreparedStatement statement =

conn.prepareStatement(queryFormat);

long startTime = System.currentTimeMillis();

for(int i=0; i<100; i++)

{

statement.setFloat(1, i/4);

ResultSet results = statement.executeQuery();

if (print)

{

showResults(results);

}

}

long stopTime = System.currentTimeMillis();

double elapsedTime = (stopTime - startTime)/1000.0;

System.out.println("Executing prepared statement " +

"100 times took " +

elapsedTime + " seconds.");

}

catch(SQLException sqle)

{

System.err.println("Error executing statement: " + sqle);

}

}

public static void doRawQueries(Connection conn, boolean print)

{

try

{

String queryFormat =

"SELECT id FROM music WHERE price < ";

Statement statement = conn.createStatement();

long startTime = System.currentTimeMillis();

for(int i=0; i<100; i++)

{

ResultSet results =

statement.executeQuery(queryFormat + i/4);

if (print)

{

showResults(results);

}

}

long stopTime = System.currentTimeMillis();

double elapsedTime = (stopTime - startTime)/1000.0;

System.out.println("Executing raw query " +

"100 times took " +

elapsedTime + " seconds.");

}

catch(SQLException sqle)

{

System.err.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 void printUsage()

{

System.out.println("Usage: PreparedStatements host " +

"dbName username password " +

"vendor [print].");

}

}

Question :explain the key output of above code

please help me to solve this question.

thanks

honey sachdeva

[3507 byte] By [honey_libraa] at [2007-10-2 16:25:03]
# 1

So, what is the problem? What's the 'key output'? The code seems

OK except i/4 won't work. It's an int, so 3/4 = 0. Change your loops

to create a float, and call setFloat() with it and append the plain

SQL string with it. You'd expect the prepared statement to be faster...

HTH,

Joe Weinstein at BEA Systems

Joe_Weinsteina at 2007-7-13 17:23:36 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 2

Also, on at least some databases, your timing test is naively inaccurate, because your assumption that the only difference between the two is the difference between the way the SQL is executed is wrong.

First, on an Oracle database, doPreparedStatement will time the 100 executions of the PreparedStatement plus the time it takes to load the PreparedStatement class, which is a significant amount of time. doStatement on the other hand, does not also measure the time to load the Statement class, because the Oracle driver will load it when DriverManager.getConnection() is called.

Second, on an Oracle database and many others, the database and/or the operating system is going to cache some of the information from disk into memory. If your database or OS caches, then your code penalizes whichever test runs first with the time to read data from disk, and not the other.

The way to fix you test is to pre-execute your queries before you start the timing loops.

StuDerbya at 2007-7-13 17:23:36 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 3

thank you for reply.

actually this is question from my exam i wrote the answer like this

"

These results will vary dramatically among database servers and drivers. For example Oracle prepared statements took only half the time that raw queries required when using a modem connection, and took only 70% of the time that raw queries required when using a fast LAN connection. Sybase times were identical in both cases.

"

The feedback as per evaluator is:

"answer is incomplete and not according to the Question.."

question is basically

explain the key output of the above program?

or

can you explain me what is the differences that occur between the execution of repeated raw queries and repeated calls to prepared statements?

thank you for your help

honey_libraa at 2007-7-13 17:23:36 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...