Give the following a try. Note: I cut and pasted bits and pieces out of some code from a project of mine and modified it. I did not compile it again or run it to check it out, but it should help you get the idea even if it doesn't run the first time.
You'll need to update some of the variable (i.e. database connect
string, userid, password, sql statement, etc...)
This gives you a generic routine to create a connection, a generic routine to dump a resultset out into a simplistic HTML table and some code executing a query.
Regards,
Byron Estes
{Code follows...}
String db_jdbcdriver = "sun.jdbc.odbc.JdbcOdbcDriver"; // or whatever driver you are using...
java.sql.Connection dbc = null;
java.sql.Statement stmt = null;
java.sql.ResultSet rs = null;
String db = "jdbc
dbc
bname"; // Put the connect String here that convforms to your driver. If you are using the JdbcOdbcDriver, the last "node" should be your database name.
String userid = "userid"; // Put the userid for the database here
String password = "password"; // Put the password for the database here
String sql = "Select * from tablename;"; // Put your sql statement here.
try {
dbc = getConnection(db_jdbcdriver,
db ,
userid,
password);
stmt = dbc.createStatement();
rs = stmt.executeQuery(sql);
dumpAnyResultSetToTable(rs, "Result Set Dump");
} catch (java.sql.SQLException sqle) {
System.out.println("Error querying database...");
} finally {
try {
// Clean up resources
if(rs != null) rs.close();
if(stmt != null) stmt.close();
if(dbc != null) dbc.close();
} catch (Exception e) {
System.out.println("Error cleaning up database connection resources...");
}
}
/**
* Utility method to handle creating a database connection.
*
* Creation date: (4/11/00 1:35:48 PM)
* @return java.sql.Connection
*/
public java.sql.Connection getConnection(String DBdriver, String url, String userid, String password) {
try{
Class.forName(DBdriver);
//DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
java.sql.Connection conn = java.sql.DriverManager.getConnection(url, userid, password);
return conn;
}catch(java.sql.SQLException se){
System.err.println(renderSQLE(se));
return null;
}catch(ClassNotFoundException ce){
System.err.println(ce.getMessage());
return null;
}
}
public String dumpAnyResultSetToTable(java.sql.ResultSet rs, String title) throws java.sql.SQLException {
StringBuffer sb = new StringBuffer();
sb.append("<TABLE border=1>");
java.sql.ResultSetMetaData rsmd = rs.getMetaData();
final int cols = rsmd.getColumnCount();
sb.append("<tr><td colspan=");
sb.append(cols + ">");
sb.append(title);
sb.append("</td></tr>");
// Load labels and column names...
// Element 0 will not be used to simplify code.
String[] labels = new String[cols + 1];
String[] names = new String[cols + 1];
for (int i = 1; i <= cols; i++) {
labels[i] = rsmd.getColumnLabel(i);
names[i] = rsmd.getColumnName(i);
}
// Build table header
sb.append("<tr>");
for (int i = 1; i <= cols; i++) {
sb.append("<th>");
sb.append(labels[i]);
sb.append("</th>");
}
sb.append("</tr>");
// Build the table data
while (rs.next()) {
sb.append("<tr>");
for (int i = 1; i <= cols; i++) {
sb.append("<td>");
sb.append(rs.getString(names[i]));
sb.append("</td>");
}
sb.append("</tr>");
}
sb.append("</table>");
return sb.toString();
}