You are on page 1of 2

Basic JSP Sample : Database Access - JDBC

Contents

Purpose Remarks Code from CountriesJdbc.jsp Code from CitiesJdbc.jsp Source Code Listings

Purpose
Connect to the database and perform database operations using JDBC.

Remarks
This sample uses Java Database Connectivity (JDBC) within a JSP to connect to the database and perform a dynamic query against an Oracle database. A JavaBean, implemented in JdbcQueryBean.java, provides the application logic. The JSPs use HTML to define the presentation format, invoking the JavaBean to generate dynamic data.

Code from CountriesJdbc.jsp


<%-- Declare the java bean for the countries to be queried --%> <jsp:useBean id="JdbcQueryBean" class="JdbcQueryBean" scope="page" /> <%-- Bean property searchCondition is not set to query countries --%> <jsp:setProperty name="JdbcQueryBean" property="searchCondition" /> <HTML> <HEAD> <TITLE>OC4J JSP Sample - Database Access Using Java Bean </TITLE> </HEAD> <BODY BGCOLOR="white"> <CENTER> <%= JdbcQueryBean.getResult() %> <BR> </CENTER> </BODY> </HTML>

Code from CitiesJdbc.jsp


<% // Store the input parameter in a local variable String searchCondition=request.getParameter("searchCondition"); if (searchCondition!=null) {

session.putValue("searchCondition",searchCondition); } else { searchCondition=(String)session.getValue("searchCondition"); } %> <%-- Declare the java bean for the cities to be queried --%> <jsp:useBean id="JdbcQueryBean" class="JdbcQueryBean" scope="page" /> <%-- Set the bean property searchCondition equal to the input parameter. --%> <jsp:setProperty name="JdbcQueryBean" property="searchCondition" value="<%= searchCondition %>" /> <HTML> <HEAD> <TITLE>OC4J JSP Sample - Database Access Using Java Bean</TITLE> </HEAD> <BODY BGCOLOR="white"> <SCRIPT language="javascript"> function back() { } </SCRIPT> <FORM> <CENTER> <%= JdbcQueryBean.getResult() %> <BR> </CENTER> </FORM> </BODY> </HTML>

Source Code Listings



CitiesJdbc.jsp CountriesJdbc.jsp JdbcQueryBean.java

<Resource name=jdbc/Test DBauth=Containertype=javax.sql.Data source maxActive=100maxIdle=30maxwait

You might also like