Repeated SQL resultset into a method

F

francan00

I have the same SQL resultset in my Database class file several times
and was wondering if I can make it into a method?


public somemethod()
{
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();
.....




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();
}
catch(Exception e)
{
}
return mydata;
}


public somemethod()
{
ResultSet results = null;
Statement statement = null;
....
mymeth();
.....
 
L

Lew

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>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top