• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Values not retrieved from the DB

 
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
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.
Regards,
Nandini
 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 (rs.next())
{
Land land = new Land(rs);
landLijst.add(land);
}

} 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
Posts: 132
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much Gert, for telling me about the MVC. It was very helpful.
Regards,
Nandini
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic