This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Servlets and the fly likes Reusing ResultSet object in Servlets. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Reusing ResultSet object in Servlets." Watch "Reusing ResultSet object in Servlets." New topic
Author

Reusing ResultSet object in Servlets.

Led Estonilo
Ranch Hand

Joined: Jan 06, 2009
Posts: 36
Good day, I have a problem with my code, im a student testing things with servlets and stuff.
the following code is a class which handles querry in a database. I like to put this to a separate class so
that i can reuse this code. the problem is when i Try to test my code, it throws a
NullPointerException. it may sound like a newbie question but can I pass the Statement Object
to JSP or other classes





this is where I try to re-use the previous code




well it compiled without errors but when i tried to run it, it
throws NullPointerException. Is my Idea possible? Did I forget
something important?
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
Apart from the problem, the design is awful. You shouldn´t be storing expensive resources as connection, statement and resultset as an instance variable. You should be acquiring and closing them in the shortest possible scope. You should map the results of interest to a collection of data transfer objects and use it instead. Lookup the DAO pattern.

Regarding to the NullPointerException: well, just read the stacktrace. The line number of the 1st line points to the line where it is been caused. Lookup that line in your code and find out which object references are possibly null while it is been accessed/invoked. If found, fix it accordingly by just instantiating it or by adding a nullcheck.
Led Estonilo
Ranch Hand

Joined: Jan 06, 2009
Posts: 36
Bauke Scholtz wrote: You shouldn´t be storing expensive resources as connection, statement and resultset as an instance variable.


ohh thats the reason everytime My mentor wants to querry a database he usually retype line 15-30 again and again. looks like i need to review what I learned back then


Regarding to the NullPointerException: well, just read the stacktrace. The line number of the 1st line points to the line where it is been caused.



is causing the NullpointerException,

If found, fix it accordingly by just instantiating it or by adding a nullcheck.

try to do that. I tried setting rs = null; or I dont know what im doing? (QQ im still a newbie)

the design is awful.

yes it is...

anyways Thank you very much for the reply, ill look up the DAO pattern and try to test some stuff.
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
Led Estonilo wrote:


is causing the NullpointerException,

If found, fix it accordingly by just instantiating it or by adding a nullcheck.

try to do that. I tried setting rs = null; or I dont know what im doing? (QQ im still a newbie)

Uhm. The value of rs is irrelevant, it is anyway going to be overriden by the outcome of doQuerry.getRs().

The only reference which can be null here is doQuerry. If it is null, then invoking getRs() would throw NullPointerException. Simply because "null" doesn't have such a method.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

I like to make sure that my persistence objects have no idea that they're being used in a web application and that my web classes have no idea
how the persistence objects work.

A simple litmus test for this is to see if any of your classes import both javax.servlet.* and java.sql.*.
If any do, then you don't have a good separation of concerns.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Led Estonilo
Ranch Hand

Joined: Jan 06, 2009
Posts: 36
Bauke Scholtz wrote:


Uhm. The value of rs is irrelevant, it is anyway going to be overriden by the outcome of doQuerry.getRs().


i mean the rs variable from the sampleClass not from where the code will be used. but i think i get it a little.


thanks for the information about the resources that can be wasted from my previous design.
Im still looking at the DOA pattern and I hope I can create a better design


Ben Souther wrote:A simple litmus test for this is to see if any of your classes import both javax.servlet.* and java.sql.*.
If any do, then you don't have a good separation of concerns.


uhmmm :?: what do you mean? seperate java.servlet.* and java.sql.* and create two seperate class which handles different jobs?

Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Led Estonilo wrote:
seperate java.servlet.* and java.sql.* and create two seperate class which handles different jobs?


Yes.
Your classes that interact directly with the database should not require a servlet container to run.
You should be able to test them from the command line, or from test scripts.
Down the road, you should be able to re-use them from a non-web interface without having to change them.

Likewise, your web classes, (servlets, filters, JSPs, listeners, etc) should not need to import any of the java.sql classes or packages.
If you're looking into DAO as Bauke Scholtz suggested, you will learn how to do this.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Reusing ResultSet object in Servlets.