Hi:
I have created the following database access bean:
/* Start of Database access Java Bean */
package javaBeanJSPSample;
import java.io.*;
import java.sql.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.ResultSet;
public class test
{
//Statement statement = connection.createStatement();
Statement statement = null;
private Connection connection=null;
private ResultSet rs = null;
String connectionURL = "jdbc:oracle:thin:@myDBURL:myDBName";
public test()
{
try {
//Load the database driver
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();;
//Get a Connection to the database
connection = DriverManager.getConnection(connectionURL, "UserId", "Password");
}
catch(Exception e){
System.out.println("Exception is ;" + e);
}
}
public ResultSet retrieveData(){
String sql = "SELECT * FROM Students WHERE stampdate = sysdate";
try{
statement = connection.createStatement();
rs = statement.executeQuery(sql);
}
catch(Exception e){
System.out.println("Exception is ;" + e);
}
//trying to return the result set object
return rs;
}
public void closeDBConnection(){
// close DB connection after retrieval is completed.
try{
rs.close();
statement.close();
connection.close();
}
catch(Exception e){
System.out.println("Exception is ;" + e);
}
}
}
/* End of Database access Java Bean */
I have been able to compile the above java bean without problem. And now I would like to reference this bean inside of a JSP page so that I do not have to write the JDBC connection code into every JSP page that I create (my aim is to have a clean JSP page devoid of much java scripting logic.)
I tried to create the following JSP page to refernce the above bean but am not able to retrieve an data - get a 500 internal server error.
/* Start of JSP page that calls above Java Bean */
<html>
<body>
<h1>Get Value from bean</h1>
<jsp:useBean id="emp" class="javaBeanJSPSample.test"/>
<%
while (rs.next()) {
%>
<%=rs.getString(1)%> |
<%=rs.getString(2)%> |
<% } %>
<% emp.closeDBConnection(); %>
</body>
</html>
/* End of JSP page that calls above Java Bean */
Appreciate any help to resolve this issue please.
Thank you
Subir