This week's book giveaway is in the OCAJP forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide 1Z0-808 and have Jeanne Boyarsky & Scott Selikoff on-line! See this thread for details.
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.
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?
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.