Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 2 of 2 FirstFirst 12
Results 16 to 24 of 24
  1. #16
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    You need to create myConnect as a class variable:
    PHP Code:
    private static Connect myConnect
    Try that, and see if that solves the issue.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  2. #17
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    ah, a little closer I think,

    Database connection established
    java.lang.NullPointerException
    at Lib.SqlMethods.<init>(SqlMethods.java:13)
    at Lib.SqlMethods.main(SqlMethods.java:24)

    it's connecting again now, but still not passing the connection over:

    line 13;
    ResultSet rs = st.executeQuery("select * from Admin WHERE AdminID = 3");


    line 24 is calling : SqlMethods() in main.

  3. #18
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Can you post your current SqlMethods class?
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  4. #19
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    Code:
    package Lib;
    
    import java.sql.ResultSet; 
    import java.sql.SQLException; 
    import java.sql.Statement; 
    public class SqlMethods{ 
    
        public SqlMethods() throws SQLException{ 
             
        Connect conn = Connect.getInstance(); 
    
            Statement st = conn.createStatement(); 
            ResultSet rs = st.executeQuery("select * from Admin WHERE AdminID = 3"); 
            while(rs.next()) 
            { 
                System.out.println(rs.getString("Username")); 
            } 
        } 
    
        public static void main(String[] argv) 
        { 
            try 
            { 
                new SqlMethods(); 
            } 
            catch (SQLException ex) 
            { 
                System.out.println("SQL Exception: " + ex.getMessage()); 
            } 
                catch (Exception e) 
                { 
                    //System.out.println("Generic Exception: " + e.getMessage()); 
                	e.printStackTrace();
                } 
        } 
    }

  5. #20
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Ack, same thing with the createStatement off of the Connect. Lol, its confusing having such similar names.
    Chain the connect.createStatement to return the conn.createStatement. That explains why we can create a statement successfully, but can't operate on it.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  6. #21
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    eak! i'm lost.

  7. #22
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Hah, sorry.
    Connection is wrapped within your Connect class. So in your SqlMethods class when it accesses the Connect class, you cannot directly access the methods of Connection. What you must do is present them yourself, so your Connect class must do this:
    PHP Code:
    public Statement createStatement()
    {
        return 
    this.conn.createStatement();  // currently your method returns null.

    In the long run, I don't think you'll end up using the createStatement in conjunction with the Connect class. I expect that you will instead use a embedded handling for the Connect, and expose a method similar to the Statement.execute[query] methods.
    If you wanted to push it to the max, you could implement Connection, Statement, DatabaseMetaData, and a few others like PreparedStatements and whatnots, and actually have a single class emulate every aspect of the Java SQL world. That doesn't sound too fun though.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  8. #23
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    that makes more sense. I'm getting close!
    with that last update, this is whats returned:


    Database connection established
    SQL Exception: No operations allowed after connection closed.


    if I comment out conn.close (); everything works. so I'm wondering why the connection gets closed so soon?


    thanks again for all your help!

  9. #24
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Quote Originally Posted by angst View Post
    that makes more sense. I'm getting close!
    with that last update, this is whats returned:


    Database connection established
    SQL Exception: No operations allowed after connection closed.


    if I comment out conn.close (); everything works. so I'm wondering why the connection gets closed so soon?


    thanks again for all your help!
    Oh yeah, thats right. Finally always tries to execute if a try block exists. Move the code from the finally block into the catch block and remove the finally block (erm, I think that makes sense lol). The nested catch will likely need to use a new variable name for the Exception since the outer catch will use e. I can see it freaking out if it goes unchanged.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 


 
Page 2 of 2 FirstFirst 12

Posting Permissions

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