Results 1 to 11 of 11
  1. #1
    KingdomX is offline Member
    Join Date
    Dec 2011
    Location
    Brazil
    Posts
    26
    Rep Power
    0

    Question Returning ResultSet from class

    I'm trying to do the following:

    Java Code:
    public static java.sql.ResultSet execute(String query){  
            ResultSet rs = null;
            try{
                rs = stmt.executeQuery(query);
            }catch(Exception e){
                //
            }
            return rs;
        }
    "query" would be = "SELECT * FROM teste"
    But when i try to access the returned "rs" variable, i always get null as if the try didn't work... Any ideas?

    I'm trying to access like this:

    Java Code:
    ResultSet rs = Db.execute("SELECT * FROM teste");
    out.print("rs: "+rs);
    Thanks in advance!

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,445
    Blog Entries
    7
    Rep Power
    20

    Default Re: Returning ResultSet from class

    Don't just hide what the possible Exception is trying to tell you; in the catch-clause do e.printStackTrace() and see why everything failed.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    KingdomX is offline Member
    Join Date
    Dec 2011
    Location
    Brazil
    Posts
    26
    Rep Power
    0

    Default Re: Returning ResultSet from class

    Oh, thx, i was wondering how to catch the errors, but that didn't solve my issue since no errors were displayed, i'll post the full class here so that you can see if anything is wrong:

    Java Code:
    package Database;
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    import java.sql.Statement;
    import java.sql.ResultSet;
    
    public class Db {
        private String driver = "com.mysql.jdbc.Driver";
        private String URL = "jdbc:mysql://localhost/gamerslust";
        private String user = "root";
        private String password = "";
        private Connection conn;  
        static Statement stmt;
        public static String test;
      
        public void Db() {  
            try{ Class.forName(driver);  
                conn = DriverManager.getConnection(URL, user, password);
                stmt = conn.createStatement();  
            }catch(Exception e){  
                e.printStackTrace();
            }  
        }
        
        public static java.sql.ResultSet execute(String query){  
            ResultSet rs = null;
            try{
                rs = stmt.executeQuery(query);
            }catch(Exception e){
                e.printStackTrace();
            }
            return rs;
        }
    }
    Thanks anyways!

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,445
    Blog Entries
    7
    Rep Power
    20

    Default Re: Returning ResultSet from class

    What did the stack trace tell you?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    KingdomX is offline Member
    Join Date
    Dec 2011
    Location
    Brazil
    Posts
    26
    Rep Power
    0

    Default Re: Returning ResultSet from class

    Nothing... it just returns the null variable to the rs var.

    Here's what i did to debug this, i created a test var on the class and had it be filled inside the TRY:

    Java Code:
        public static java.sql.ResultSet execute(String query){  
            ResultSet rs = null;
            try{
                rs = stmt.executeQuery(query);
                test = ("test");
            }catch(Exception e){
                e.printStackTrace();
            }
            return rs;
        }
    That var there also returns null, but not if i move it before the return rs:
    Java Code:
        public static java.sql.ResultSet execute(String query){  
            ResultSet rs = null;
            try{
                rs = stmt.executeQuery(query);
            }catch(Exception e){
                e.printStackTrace();
            }
            test = ("test");
            return rs;
        }
    So, on my understanding the code inside the TRY isn't being executed... Any ideas on this?

    EDIT: However, if i add class var and set it the CATCH, it does fill, so i can assume it's returning an error?
    Last edited by KingdomX; 12-22-2011 at 11:48 PM.

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Returning ResultSet from class

    i can assume it's returning an error?
    Yes, it is throwing an exception. The API docs for executeQuery() are very clear about this:

    "Returns: a ResultSet object that contains the data produced by the given query; never null
    Throws: SQLException - if a database access error occurs, this method is called on a closed Statement or the given SQL statement produces anything other than a single ResultSet object"

    So the question is that raised by Jos earlier: what does the stack trace say? If you can't see the stack trace for some reason, then that has to be addressed. (Where are you looking? Has System.err been redirected?)

  7. #7
    KingdomX is offline Member
    Join Date
    Dec 2011
    Location
    Brazil
    Posts
    26
    Rep Power
    0

    Default Re: Returning ResultSet from class

    Sorry, i have no idea what you guys are talking about... i'm new to Java and i'm just trying to get a Database class working so that i can start developing my website. I used to use PHP, but it is much simpler, so i'm having a few bumps.

    Anyways, i managed to figure something out. I was initially trying to set the database connection on the constructor method, but nothing was happening... So instead of doing this, i created a new Method:


    Java Code:
    package Database;
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    import java.sql.Statement;
    import java.sql.ResultSet;
    
    public class Db {
        private String driver = "com.mysql.jdbc.Driver";
        private String URL = "jdbc:mysql://localhost/gamerslust";
        private String user = "root";
        private String password = "";
        public Connection conn;  
        public Statement stmt;
      
        public void Db() {  
            try{
                Class.forName(driver);  
                this.conn = DriverManager.getConnection(URL, user, password);
                this.stmt = conn.createStatement();  
            }catch(Exception e){  
                e.printStackTrace();
            }  
        }
      
        public void set() {  
            try{
                Class.forName(driver);  
                this.conn = DriverManager.getConnection(URL, user, password);
                this.stmt = conn.createStatement();  
            }catch(Exception e){  
                e.printStackTrace();
            }  
        }
    }
    As you can see, the set method is exactly just like the constructor method, but instead it actually filled the fields as i initially intended. Here's how i'm displaying the results:

    Java Code:
                    Db Db = new Db();
                    out.print("Db.conn: "+Db.conn+"<br />"); // DISPLAYS: Db.conn: null
                    out.print("Db.stmt: "+Db.stmt+"<br />"); // DISPLAYS: Db.stmt: null
                    Db.set();
                    out.print("Db.conn: "+Db.conn+"<br />"); // DISPLAYS: Db.conn: com.mysql.jdbc.JDBC4Connection@283d477d
                    out.print("Db.stmt: "+Db.stmt+"<br />"); // DISPLAYS: Db.stmt: com.mysql.jdbc.StatementImpl@3e3d4407
    So, why won't the constructor work the way i'm trying to make it work... Any ideas?

    Thanks for the input so far!

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: Returning ResultSet from class

    How are you running this code?
    Because if it is getting into the catch block at all then that stack trace is going somewhere...

    Also your general db access structure is not so good.
    Holding onto a connection on a single threaded (ie desktop) system is OK.
    Having standard Statements around is OK, but only really helpful for PreparedStatements (ones that take parameters).
    Passing around ResultSets like that is not so good. It leaves you prone to resource leakage.
    You should get the result set and process it in the same place, then close it.
    That processing could be in a method call, but the stuff you need from the RS should be converted into objects that mean something in your app (otherwise known as business objects, or your model) and it is those that should be returned.
    eg
    Java Code:
    public List<MyClass> getSomeData(<some parameters possibly>) {
        ResultSet rs = null;
        List<MyClass> mc = new ArrayList<MyClass>();
        try {
            // Run the query and get the RS
            // Populate the List
        } catch (SQLException ex) {
            // Log the message and probably rethrow, after all something has gone wrong
        } finally {
            // This is the important bit!
            if (rs != null) {
                // Close it!
            }
        }
        return mc;
    }
    That's the basic structure.

  9. #9
    KingdomX is offline Member
    Join Date
    Dec 2011
    Location
    Brazil
    Posts
    26
    Rep Power
    0

    Default Re: Returning ResultSet from class

    Oh, thanks, that's really helpfull, i didn't realize that! Resource leak - Wikipedia, the free encyclopedia

    I understand what you pointed out... I'm gonna fix it somehow then! Thanks!

  10. #10
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default Re: Returning ResultSet from class

    So, why won't the constructor work the way i'm trying to make it work... Any ideas?

    Java Code:
    public void Db(){
    
    }
    Is a method, not a constructor.

    Java Code:
    public Db(){
    
    }
    is a constructor. See Providing Constructors for Your Classes (The Java™ Tutorials > Learning the Java Language > Classes and Objects)

  11. #11
    KingdomX is offline Member
    Join Date
    Dec 2011
    Location
    Brazil
    Posts
    26
    Rep Power
    0

    Default Re: Returning ResultSet from class

    Oh, thanks for that! It worked!

    Thanks guys, everything is good now! :D

Similar Threads

  1. ResultSet GUI
    By razmyasdfg in forum JDBC
    Replies: 4
    Last Post: 03-15-2011, 01:51 PM
  2. Returning/Handling MySQL ResultSet
    By dxg in forum JDBC
    Replies: 4
    Last Post: 01-26-2011, 09:00 AM
  3. Returning a ResultSet to custom JTable Model
    By Kenjitsuka in forum New To Java
    Replies: 9
    Last Post: 10-16-2010, 09:17 PM
  4. Help with Resultset
    By xxAlemanxx in forum JDBC
    Replies: 6
    Last Post: 06-24-2008, 11:09 AM
  5. ResultSet to XML
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-14-2008, 09:50 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •