File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JSP and the fly likes Values not retrieved from the DB Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Values not retrieved from the DB" Watch "Values not retrieved from the DB" New topic

Values not retrieved from the DB

Nandini Sriram
Ranch Hand

Joined: Jul 04, 2003
Posts: 132
Can someone tell me if this is correct. I am trying to fetch values from the database using servlet/JSP. I have a servlet, which obtains the DB connection. I am using the connection object (which is in the servlet-class) in my JSP to obtain the statement & resultset objects and finally execute the query. When I tried this way the results are not fetched. So I was wondering if the method of obtaining the connection object (servlet to jsp)is wrong or it is a JDBC issue.
Please explain.

Discipline, Dedication and Determination define Destiny.
Gert Cuppens
Ranch Hand

Joined: Jul 13, 2003
Posts: 87
The way you're working seems correct. If you're following the MVC model 2, you should let servlets and JSP's collaborate.
The Model View Controller contains the following elements :
- JSP : takes care of the presentation and nothing else. Hence, this is the View.
- Model : these are the classes that refer to the objects known by the users of you application : Customer, Invoice, Product. So, these are the Java Beans.
- Controller : the controller takes care of the requests, decides upon the request which response must be given. The controller is the servlet or a series of servlets.
Then there is one more thing you ought to know : the MVC Model 2 should work with DAO's or Data Access Objects. Each Java Bean should have a corresponding DAO (so, if your application has a Customer, there should be a CustomerDAO as well). The DAO's contain the data layer of your application. They contain the SQL statements necessary to extract the records from the database, and translate the resultsets into java beans.
I'll give an example :
The LandDAO has a method zoekLand() (searchCountry()) which returns an ArrayList of country; I'm not giving all the lines of course; it would become unreadable
public ArrayList zoekLand()
throws SQLException
ArrayList landLijst = new ArrayList();
StringBuffer zoekString = new StringBuffer();
zoekString.append("select * from land");

try {
con = getConnection();
ps = con.prepareStatement(zoekString.toString());
rs = ps.executeQuery();
while (
Land land = new Land(rs);

} catch (SQLException SQLEx)
return landLijst ;
} // end zoekLand
The ArrayList is returned to the servlet which has used the zoekLand() method; the ArrayList is then placed in the session of the request
ArrayList landLijst = landDao.zoekLand();
HttpSession session = request.getSession(false);
session.setAttribute("landLijst", landLijst);
With the requestDispatcher you give the control to the JSP. And how does the JSP get the values of the ArrayLIst ? With these lines of code which you should place at the beginning of the JSP :
<%@ page import = "org.brukkerlin.adres.Land" %>
<%@ page import = "java.util.Iterator" %>
<jsp:useBean id="servletPath" class="java.lang.String" scope="request"/>
<jsp:useBean id="landLijst" class="java.util.ArrayList" scope="session"/>
Nandini Sriram
Ranch Hand

Joined: Jul 04, 2003
Posts: 132
Thank you very much Gert, for telling me about the MVC. It was very helpful.
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
subject: Values not retrieved from the DB
It's not a secret anymore!