Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JDBC & JSP

 
Danish Shaukat
Ranch Hand
Posts: 341
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!
I'm working on jsp these days. Here is what i have.
I make a query to a database. I know that all information(results obtained) will be encapsulated in a java bean.
I think the query should be made in a servlet but i'm not sure.
Now my question is that wether the query should be made in the java bean or in a servlet?
Thanks in advance.
 
maha anna
Ranch Hand
Posts: 1467
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From my experience , don't write any database related code in servlets which means NO SQL CODE in servlets. Let the servlet just call another Model class for this task. For example if you want to see all the employees in the 'Employees' table in your database, you call the corresponding method of the Database class from servlet like this.
EmployeesBank.getAllEmployees();
Which may return a javabean which has the resultset of the query. The servlet just 'uses' the returned object. Maximum you can send the 'Connection' object as an argument to the Database class like this
regds
maha anna
 
amit vaid
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi
I'm new at this but i totally agree with maha anna that using jsp u should use beans for making database queries. I have used servlet's a lot but even than most often than not i used to have a seperate class for database queries.
well best of luck!
 
rich werth
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
I'm doing the same thing. I query in the bean and am able to get the resultSet back to the servlet but not back to a jsp page.
Here is what I have so far.
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String tableName = "";
try {
tableName = request.getParameter("table");
db.setTable(tableName);
db.connect();
rs=db.viewItems();
request.setAttribute("theResult", rs);
}
catch(Exception e) {
e.printStackTrace();
}
ServletContext sc = getServletContext();
String url = "/loop.jsp";
RequestDispatcher rd = sc.getRequestDispatcher(url);
request.setAttribute("theResult", joe);
rd.forward(request, response);
}

Can someOne please show me where I'm going wrong. I have looked in a few books and a few webSites but I haven't seemed to find any examples of this.
Any help is greatly apreciated
 
Stanley Tan
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't put the ResultSet in the session. Besides, it closes as soon as the connection dies. Put the data in a JavaBean and then put the bean in the session. Then you can access it from your JSP page.
I haven't tried this yet, but I assume you can also use a CachedRowSet (which is disconnected from the database) and put it in the session.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic