Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / October 2007

Tip: Looking for answers? Try searching our database.

Repeated SQL resultset into a method

Thread view: 
francan00@yahoo.com - 02 Oct 2007 23:20 GMT
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();
....
Lew - 02 Oct 2007 23:37 GMT
> 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;
[quoted text clipped - 4 lines]
> 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()
> {
[quoted text clipped - 3 lines]
>     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>

Signature

Lew



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.