Repeated SQL resultset into a method

Discussion in 'Java' started by francan00, Oct 2, 2007.

  1. francan00

    francan00 Guest

    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();
    .....
     
    francan00, Oct 2, 2007
    #1
    1. Advertisements

  2. francan00

    Lew Guest

    Not legal Java.
    Be sure to check the result of the call to 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".)
    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.
    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).
    This is not legal Java. Please provide a complete example that compiles.
    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, Oct 2, 2007
    #2
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.