Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-11-2010, 11:47 PM   PM User | #16
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,653
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
You need to create myConnect as a class variable:
PHP Code:
private static Connect myConnect
Try that, and see if that solves the issue.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Old 11-11-2010, 11:53 PM   PM User | #17
angst
Senior Coder

 
angst's Avatar
 
Join Date: Apr 2004
Location: Toronto, Ontario
Posts: 2,112
Thanks: 15
Thanked 122 Times in 122 Posts
angst is on a distinguished road
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.
angst is offline   Reply With Quote
Old 11-11-2010, 11:54 PM   PM User | #18
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,653
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Can you post your current SqlMethods class?
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Old 11-11-2010, 11:55 PM   PM User | #19
angst
Senior Coder

 
angst's Avatar
 
Join Date: Apr 2004
Location: Toronto, Ontario
Posts: 2,112
Thanks: 15
Thanked 122 Times in 122 Posts
angst is on a distinguished road
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();
            } 
    } 
}
angst is offline   Reply With Quote
Old 11-12-2010, 12:12 AM   PM User | #20
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,653
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Old 11-12-2010, 12:19 AM   PM User | #21
angst
Senior Coder

 
angst's Avatar
 
Join Date: Apr 2004
Location: Toronto, Ontario
Posts: 2,112
Thanks: 15
Thanked 122 Times in 122 Posts
angst is on a distinguished road
eak! i'm lost.
angst is offline   Reply With Quote
Old 11-12-2010, 03:21 AM   PM User | #22
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,653
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Old 11-12-2010, 01:39 PM   PM User | #23
angst
Senior Coder

 
angst's Avatar
 
Join Date: Apr 2004
Location: Toronto, Ontario
Posts: 2,112
Thanks: 15
Thanked 122 Times in 122 Posts
angst is on a distinguished road
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!
angst is offline   Reply With Quote
Old 11-12-2010, 05:38 PM   PM User | #24
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,653
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:15 PM.


Advertisement
Log in to turn off these ads.