Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Retrieving contents of more than one table in jsp

 
Saumyaraj Zala
Ranch Hand
Posts: 65
AngularJS Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:-

 
K. Tsang
Bartender
Posts: 3457
14
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64970
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should not be doing Java code in a JSP.

In any case, this is a JDBC question and has been moved appropriately.
 
Saumyaraj Zala
Ranch Hand
Posts: 65
AngularJS Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 65
AngularJS Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic