Re: Repeated SQL resultset into a method
francan00@yahoo.com wrote:
I have the same SQL resultset in my Database class file several times
and was wondering if I can make it into a method?
Yes.
public somemethod()
Not legal Java.
{
ResultSet results = null;
Statement statement = null;
....
//next two lines are repeated several times in different methods in
this class file:
results = statement.executeQuery("select sport from typetable");
results.next();
Be sure to check the result of the call to next()!
.....
My attempt outputs no data. Please advise.
public boolean mymeth()
{
try {
ResultSet results = null;
Statement statement = null;
results = statement.executeQuery("select sport from typetable");
boolean mydata = results.next();
And where are you putting the results? I don't mean the java.sql.ResultSet
itself, I mean where are you putting the data that it returned?
(Hint: the answer is "nowhere".)
}
catch(Exception e)
{
Never omit exception handling. If you want to skip it on Usenet for
pedagogical reasons, comment that you're skipping exception-handling for
pedagogical reasons.
}
return mydata;
}
All this method returns is whether there's at least one row of data in the
ResultSet (true if there is, false if there isn't).
public somemethod()
This is not legal Java. Please provide a complete example that compiles.
{
ResultSet results = null;
Statement statement = null;
....
mymeth();
.....
My attempt outputs no data. Please advise.
You haven't shown us the part where data would be output, so we can't say why
it isn't. Or is there no such part in the code that you elided? Try copying
the data into some reasonable structure and using some output command(s) on
it. None of the code you show us retrieves any data from the ResultSet, much
less tries to output it.
Read up on SSCCEs, "Short, Self-Contained Compilable Example"s.
<http://www.physci.org/codes/sscce.html>
--
Lew