Prentice Hall and Sun Microsystems. Personal use only; do not redistribute.
18.3 Some JDBC Utilities
477
Listing 18.6 DatabaseUtilities.java (continued)
try {
Statement statement = connection.createStatement();
// Drop previous table if it exists, but don't get
// error if it doesn't. Thus the separate try/catch here.
try {
statement.execute("DROP TABLE " + tableName);
} catch(SQLException sqle) {}
String createCommand =
"CREATE TABLE " + tableName + " " + tableFormat;
statement.execute(createCommand);
String insertPrefix =
"INSERT INTO " + tableName + " VALUES";
for(int i=0; i
statement.execute(insertPrefix + tableRows[i]);
}
if (close) {
connection.close();
return(null);
} else {
return(connection);
}
} catch(SQLException sqle) {
System.err.println("Error creating table: " + sqle);
return(null);
}
}
public static void printTable(String driver,
String url,
String username,
String password,
String tableName,
int entryWidth,
boolean close) {
String query = "SELECT * FROM " + tableName;
DBResults results =
getQueryResults(driver, url, username,
password, query, close);
printTableData(tableName, results, entryWidth, true);
}
/** Prints out all entries in a table. Each entry will
* be printed in a column that is entryWidth characters
* wide, so be sure to provide a value at least as big
* as the widest result.
*/
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