*
The moose likes JDBC and the fly likes Need assistance with approach to problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Need assistance with approach to problem" Watch "Need assistance with approach to problem" New topic
Author

Need assistance with approach to problem

Terry Chambers
Greenhorn

Joined: May 11, 2009
Posts: 17
Situation:

I have a database query to complete which will return a result set that I will then parse through and retrieve the values from the set.
I have created a class called "obQuery". As part of the constructor, I am generating the objects to create the query and then have created a method called "getResultSet", within the main class, that generates the "ResultSet" object from the SQL statement.

When I instantiate the "obQuery" object, I'd like to be able to retrieve the results of the ResultSet. I am lost though because, if I call the "getResultSet" method, that method has to know which SQL statement to run and the calling code does not know that value.

How can I create an empty ResultSet object and populate it with the output of the "getResultSet" code, without having to pass in the values to the method?

Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3064
    
  33

Terry Chambers wrote: When I instantiate the "obQuery" object, I'd like to be able to retrieve the results of the ResultSet.

Why would you want to do all the operations in the constructor itself?

Terry Chambers wrote: I am lost though because, if I call the "getResultSet" method, that method has to know which SQL statement to run and the calling code does not know that value.

You are passing- queryURL, queryTerm to the constructor and of which I suppose queryTerm is the SQL query to be executed?

Terry Chambers wrote: How can I create an empty ResultSet object and populate it with the output of the "getResultSet" code, without having to pass in the values to the method?

Why would you want to create an empty object and then populate it with results when you can get the object from the Statement? And moreover ResultSet is an Interface.

Terry Chambers wrote:

In this method- though you are passing the queryTerm, you arent using it in the method. You could replace the string in the executeQuery() with the queryTerm being passed

Mohamed Sanaulla | My Blog
Costi Ciudatu
Ranch Hand

Joined: Oct 24, 2006
Posts: 74
I'm not sure if I understood your actual question: is it something like: "how to make use of the variables passed to the constructor in the getResultSet() method, so that once created, a dbQuery instance can return the result by simply calling getResultSet() -- with no parameters ?"
If so, I'll try to answer this question alone and just remind you that a class can hold state as instance variables.
Therefore, you can keep the queryTerm you get in the constructor and the Statement you are creating as instance variables
(I'm just pointing out a solution for your particular concern, assuming I got it right, as storing a Statement as an instance variable is a... "second best" practice, so to speak)



Again, as Mohamed pointed out, there is a lot to reconsider about your code, but I'd say let's clarify them one at a time...
Terry Chambers
Greenhorn

Joined: May 11, 2009
Posts: 17
Thanks to you both. I'll go back and look at how I am approaching this problem.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37900
    
  22
I think this thread would sit better on our databases forum. Moving discussion.
Ravi Kiran Va
Ranch Hand

Joined: Apr 18, 2009
Posts: 2234

As i can see taht you are returning the ResultSet itself from the Method .
This is a bad practice , you should not keep ResultSet open for that much long . (Because this involves Database related stuff also Open )






Save India From Corruption - Anna Hazare.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Need assistance with approach to problem
 
Similar Threads
NullPointerException At Statement stmt = conn.createStatement();
an easy test question relating to Bean Management Persistent
Datasource Connection
JDBC Connection (or "I Am At My Wits End")
Please Help! I Have Worked On IT For Days.