Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

sample jsp page calling database access bean  RSS feed

 
Subir Mukherjee
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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



 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65832
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please be sure to use code tags when posting code to the forums. Unformatted code is extremely hard to read and many people that might be able to help you will just move along to posts that are easier to read. Please read this for more information.

You can go back and change your post to add code tags by clicking the button on your post.

 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65832
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You shouldn't be referencing this bean from a JSP. JSPs should be used for views only. A page controller servlet would be much more appropriate.
 
Subir Mukherjee
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Subir Mukherjee wrote:Hi:

I have created the following database access bean:

/* Start of Database access Java Bean */

/* 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 */

/* End of JSP page that calls above Java Bean */

Appreciate any help to resolve this issue please.

Thank you
Subir



 
vikhyath reddy
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You cannot directly access rs.next() without calling a call to the method in bean . Instead try calling any method in the bean . Just by involving the useBean tag doesn't give you the beans methods (its not some kind of an import ) . call some method in the bean --- <% beanId_Name.method(); %> will give you the access to the method ....


~ Happy coding ~
 
Led Estonilo
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

well I have the same questions on this topic, instead of creating a new thread I decided to bump this thread because I still have questions.

vikhyath reddy wrote:You cannot directly access rs.next() without calling a call to the method in bean . Instead try calling any method in the bean . Just by involving the useBean tag doesn't give you the beans methods (its not some kind of an import ) . call some method in the bean --- <% beanId_Name.method(); %> will give you the access to the method ....



do you mean by adding <% =emp.retrieveData() %>, one can access ResultSet object? and we can use rs.getString on a JSP page?


 
vikhyath reddy
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
no you cannot . In order for you to hold a result set type you need to assign the call within the method to the result set object in the jsp page. This process is complex and provides you with little variety . Every time you need a different database functionality you need to recompile your bean class and restart your server . So instead try including the jdbc code within the jsp page if you have a need of accessing different databases with the results being different from each other .



~ Happy coding ~
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65832
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
vikhyath reddy wrote:So instead try including the jdbc code within the jsp page ...

This is not good advice.

Never include JDBC code in a JSP page. That is considered an incredibly poor practice by every Java web developer with any amount of experience.

In fact, you shouldn't be including Java code at all in your JSP pages, but JDBC code is especially egregious.
 
vikhyath reddy
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
looks like you have changed your post from "its rare to see..... " to 'not a good advice' . Well i am still learning and i am hardly 20 years.Been 2 months that i started working with java web. You are experienced enough bear... and this forum is meant for everybody i guess , not just for the ph.d's !
 
Led Estonilo
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



ok so the best way now is to use JSTL / EL ?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65832
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
vikhyath reddy wrote:looks like you have changed your post ...
Yeah, I thought it came out too harsh and that was not my intention. Yes, these forums are for everyone, but it's important to make sure that the advice given represents best practices. If advice is given that will cause someone to head down the road of poor practices you can expect for it to be corrected.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65832
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Led Estonilo wrote:ok so the best way now is to use JSTL / EL ?

With the advent of JSP 2.0, the JSTL and EL are the accepted means to create JSP pages in place of Java scriptlets. But it's more than just that... if web applications are not properly structured, it's difficult to impossible to create web apps using modern practices.

With regard to JDBC and database access, such actions should be coded in UI-agnostic Java classes as part of the model of an MVC pattern. Perhaps this article will be helpful.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!