File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Need assistance with approach to problem

 
Terry Chambers
Greenhorn
Posts: 17
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 3159
33
Google App Engine Java Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Costi Ciudatu
Ranch Hand
Posts: 74
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to you both. I'll go back and look at how I am approaching this problem.
 
Campbell Ritchie
Sheriff
Pie
Posts: 47228
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think this thread would sit better on our databases forum. Moving discussion.
 
Ravi Kiran Va
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 )





 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic