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.
<%@ 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:
- JDBC Driver: <%= jdbcDriver==null?"No driver specified.":jdbcDriver %>
- Database URL: <%= dbURL==null?"No URL specified":dbURL %>
- SQL query: <%= sqlQuery==null?"No SQL query":sqlQuery %>
- Username: <%= username==null?"No username supplied":username %>
- Password: <%= passwd==null?"No password supplied":passwd %>
<%
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();
%>