Re: Can a ResultSet be passed back to a main program
On 11-10-10 03:28 AM, Linus Flustillbe wrote:
On 2011-10-10, Linus Flustillbe <admin@nacs.dyndns-office.com> wrote:
I will continue to experiment and will post my findings if I can figure
this out. Is what I am trying to do even possible? Any assistance
would be appreciated.
Thanks
I have been succesful.... here is my code
This is the class that does the connection
package mainClasses;
import java.sql.*;
public class TableConnect {
public Statement getStatement() {
return statement;
}
public void setStatement(Statement statement) {
this.statement = statement;
}
private Statement statement;
public TableConnect() throws ClassNotFoundException,
SQLException
{
Class.forName("oracle.jdbc.driver.OracleDriver");
Statement tstate;
String url = "jdbc:oracle:thin:@xxxxxxx1:1521:xxxxxxx";
Connection conn =
DriverManager.getConnection(url,"xxxxxxx",
"xxxxxxx");
tstate = conn.createStatement();
setStatement(tstate);
}
public ResultSet GetResultSet(Statement stmt, String tableName)
throws SQLException {
String query = "select * from " + tableName ;
return stmt.executeQuery(query);
}
}
====================
And here is my testing code
package testing;
import java.sql.*;
import mainClasses.*;
public class ConnectionTest {
public static void main(String[] args) throws
ClassNotFoundException, SQLException
{
Class.forName("oracle.jdbc.driver.OracleDriver");
String tableName="LU_MEDIA_USAGE";
TableConnect myConnection = new TableConnect();
Statement stmt = myConnection.getStatement();
ResultSet rset = myConnection.GetResultSet(stmt, tableName);
while (rset.next()) {
System.out.println (rset.getString(1));
}
stmt.close();
}
}
When I run ConnectionTest, I get the following output
CL
[ SNIP ]
TR
These are just 3 character codes that I am using as "enum" types
for my application. Suggestions for improvement are welcome.
Like I would have preferred to do one method call and have the stmt
be internal to that i.e. without needing a variable in the main program
called stmt that gets set from a method call then then passed to a
another method. Maybe if I sleep on it, the answer will come to me.
The above that you noted is certainly one improvement to make; it's good
that you remarked upon it. As a rule of thumb, if a variable is internal
to class A, it's a code smell to retrieve it in class B with a getter
method, only to simply supply it as a parameter in another method called
on A by B.
On this point I think you'll eventually see by looking at
TableConnect.java that you already made the statement available to any
other method in the class, and there was no need for that parameter on
the GetResultSet method.
General nomenclature notes in Java: method names in Java are also
camel-case, which you've been using elsewhere.
In a more general sense, when working with JDBC or any other API where
you acquire, use and dispose of connnections, it is fairly common
practise to write up a little utility class that has a few methods to do
these things. You're on the way to doing that with TableConnect. Such a
class would not contain any logic for specific statements or result
sets, usually - that kind of thing is business logic. So I would pull
everything pertaining to Statement and ResultSet out of TableConnect,
and for this small test case keep it in the main class.
Also, get in the habit of not creating your query strings by
concatenating static strings and supplied variables. Here in this
example it's a non-issue, but for a GUI app or a web app, maybe sooner
or later that variable that is meant to be a table name is a
user-supplied string that contains something else entirely. Word to the
wise.
AHS
--
I tend to watch a little TV... Court TV, once in a while. Some of the
cases I get interested in.
-- O. J. Simpson