Java Reference
In-Depth Information
The example in Listing 8-1 is a simple JDBC, with a couple of lines of additional code that calculate the
start and stop times of the query so that the elapsed can be calculated. By commenting out the
CREATE INDEX and DROP INDEX lines, speed improvement can easily be calculated.
Listing 8-1: Creating and dropping indexes
package java_databases.ch04;
import java.sql.*;
public class PrintIndexedResultSet{
public static void main(String args[]){
String query =
"SELECT STATE, COUNT(STATE) FROM MEMBER_PROFILES GROUP BY STATE";
PrintIndexedResultSet p = new PrintIndexedResultSet(query);
}
public PrintIndexedResultSet(String query){
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection ("jdbc:odbc:Members");
Statement stmt = con.createStatement();
stmt.executeUpdate("CREATE INDEX STATE_INDEX ON
MEMBER_PROFILES(STATE)");
java.util.Date startTime = new java.util.Date();
ResultSet rs = stmt.executeQuery(query);
ResultSetMetaData md = rs.getMetaData();
int nColumns = md.getColumnCount();
for(int i=1;i<=nColumns;i++){
System.out.print(md.getColumnLabel(i)+((i==nColumns)?"\n":"\t"));
}
while (rs.next()) {
Search WWH ::




Custom Search