When testing a newly configured datasource we suggest using some very basic JDBC client code embedded in a JSP page. First of all, you should create an exploded WAR archive under the deploy directory which is simply a folder named "
". In this folder, create a text document named client.jsp and paste the code below:
<%@page contentType="text/html"
import="java.util.*,javax.naming.*,javax.sql.DataSource,java.sql.*"
%>
<%
DataSource ds = null;
Connection con = null;
PreparedStatement pr = null;
InitialContext ic;
try {
ic = new InitialContext();
ds = (DataSource)ic.lookup( "java:/DefaultDS" );
con = ds.getConnection();
pr = con.prepareStatement("SELECT USER_ID, PASSWD FROM JBM_USER");
ResultSet rs = pr.executeQuery();
while (rs.next()) {
out.println("<br> " +rs.getString("USER_ID") + " | " +rs.getString("PASSWD"));
}
rs.close();
pr.close();
}catch(Exception e){
out.println("Exception thrown " +e);
}finally{
if(con != null){
con.close();
}
} %>
<%@page contentType="text/html"
import="java.util.*,javax.naming.*,javax.sql.DataSource,java.sql.*"
%>
<%
DataSource ds = null;
Connection con = null;
PreparedStatement pr = null;
InitialContext ic;
try {
ic = new InitialContext();
ds = (DataSource)ic.lookup( "java:/DefaultDS" );
con = ds.getConnection();
pr = con.prepareStatement("SELECT USER_ID, PASSWD FROM JBM_USER");
ResultSet rs = pr.executeQuery();
while (rs.next()) {
out.println("<br> " +rs.getString("USER_ID") + " | " +rs.getString("PASSWD"));
}
rs.close();
pr.close();
}catch(Exception e){
out.println("Exception thrown " +e);
}finally{
if(con != null){
con.close();
}
} %>
Copy to Clipboard
Copied!
Toggle word wrap
Toggle overflow
. A list of users and password should show up as a result of the JDBC query:
dynsub | dynsub
guest | guest
j2ee | j2ee
john | needle
nobody | nobody
dynsub | dynsub
guest | guest
j2ee | j2ee
john | needle
nobody | nobody
Copy to Clipboard
Copied!
Toggle word wrap
Toggle overflow