aspose file tools*
The moose likes JDBC and the fly likes Retrieving contents of more than one table in jsp Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Retrieving contents of more than one table in jsp" Watch "Retrieving contents of more than one table in jsp" New topic
Author

Retrieving contents of more than one table in jsp

Saumyaraj Zala
Ranch Hand

Joined: Aug 06, 2012
Posts: 56

I have two tables in my database named "branch" and "course". I want to display contents of one column of each table in a select option. I can retrieve one column successfully but when i try to retrieve data from more than one table it gives me an Exception which says "result set is closed".

Here's my code:-



Why do we fall? So that we can learn to pick ourselves up.
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2505
    
    8

Which result set is closed: "r" or "r1"? One way to avoid this is to just use one result set variable. Populate 1 select list at a time and reuse the same result set variable.

By the way, you really should do the query in a servlet.


K. Tsang JavaRanch SCJP5 SCJD/OCM-JD OCPJP7 OCPWCD5 OCPBCD5
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61421
    
  67

You should not be doing Java code in a JSP.

In any case, this is a JDBC question and has been moved appropriately.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Saumyaraj Zala
Ranch Hand

Joined: Aug 06, 2012
Posts: 56

By the way, you really should do the query in a servlet.

But the java code here helps in making my jsp page. I use the servlets to do computations and according to the result just transfer the control of a web page. How can i implement this as a servlet? Am i going wrong?
Saumyaraj Zala
Ranch Hand

Joined: Aug 06, 2012
Posts: 56

K. Tsang wrote:Which result set is closed: "r" or "r1"? One way to avoid this is to just use one result set variable. Populate 1 select list at a time and reuse the same result set variable.



After the making my first select tag i am closing the first result set and after making second i am closing the second result set.
Frederik Nielsen
Greenhorn

Joined: Aug 25, 2013
Posts: 14
This is what happens when you 100% mix server code with client code.

jsp pages are ONLY for displaying data. you are not supposed to make database request directly in them. Just because you can, does not mean you should.

- What you should do is to make a Servlet Filter that gets called before the jsp is loaded.
- In Servlet Filter - make a call to a Servlet Controller (or do it directly in the filter if you are lazy) to get the data you want to display on the jsp.
- In Servlet Controller - make a call to the Data Access Object classes needed
- In Data Access Object class - get a connection from Database class, then get the data, close connection and return it
- when the data has been returned back to the Servlet Filter, can you make a session or just pass the data in the request to the jsp.
- In JSP - read data from session or request and display it.

I know this might seam like overkill, but it is just good practice to seperate the different parts to the place they belong.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Retrieving contents of more than one table in jsp