Using JSP to retrieve database data with JDBC

Make a selection

Choose a JDBC driver and a database name from the drop down lists below. Note that to use the 'demoPool' connection pool option, you must configure your weblogic.properties file. The 'demoPool' connection pool properties are provided by default, but they may be commented out.

JDBC driver :
Database URL / Connection Pool :
Username :
Password :
SQL Query :

<%@ page import=" javax.naming.*, java.util.*, java.sql.*, weblogic.common.* " %> <% if ("POST".equals(request.getMethod())) { String jdbcDriver = (String) request.getParameter("jdbcDriver"); String dbURL = (String) request.getParameter("dbURL"); String sqlQuery = (String) request.getParameter("sqlQuery"); String username = (String) request.getParameter("username"); if (username != null && username.equals("")) username=null; String passwd = (String) request.getParameter("passwd"); if (passwd != null && passwd.equals("")) passwd = null; %>

Results from previous query:

Here are the results from the previous SQL query using the these parameters:

<% Connection conn = null; Statement stmt = null; ResultSet rs = null; try { Class.forName(jdbcDriver).newInstance(); if ((username != null) && (passwd != null)) conn = DriverManager.getConnection(dbURL, username, passwd); else conn = DriverManager.getConnection(dbURL, null); stmt = conn.createStatement(); rs = stmt.executeQuery(sqlQuery); ResultSetMetaData rsmd = rs.getMetaData(); int numCols = rsmd.getColumnCount(); %>

<% for (int i = 1; i <= numCols; i++) { %> <% } %> <% while (rs.next()) { %> <% for (int i = 1; i <= numCols; i++) { %> <% } %> <% } } catch (Exception e) { %>

There was an error executing or processing the query:
Exception: <%= e %>

<%= getStackTraceAsString(e) %>
<% } finally { try { rs.close(); stmt.close(); conn.close(); } catch (Exception e) { out.print("There was an error closing the database connection"); out.print("
Exception: " +e); out.print("
"+getStackTraceAsString(e)+"
"); } } } %>
<%= rsmd.getColumnLabel(i) %>
<%= rs.getString(i) %>

Copyright (c) 1999 by BEA Systems, Inc. All Rights Reserved. <%! String getStackTraceAsString(Exception e) { // Dump the stack trace to a buffered stream, then return it's contents // as a String. This is useful for printing the stack to 'out'. ByteArrayOutputStream ostr = new ByteArrayOutputStream(); e.printStackTrace(new PrintStream(ostr)); return(ostr.toString()); } %>