Xperiaz X
Xperiaz X

Reputation: 226

Passing SQL connections to objects

Note: Not a java EE question

In java, I need to pass an SQL Connection from one class to another as a parameter. But it seems that the receiving class gets the Connection object but it is null and not connected.

I read java EE uses a pool. But my project is small with a localhost connection using the Driver manager of JDBC. Also I read java does no pass by reference.

Any way I can pass a live connection from one class to another?

EDIT: IN CLASS CONNECT

try
  {
      Class.forName("com.mysql.jdbc.Driver");
      connect = DriverManager.getConnection(
                "jdbc:mysql://localhost/MYDATABASE?user="+username+"&password="+password);
return connect;
  }catch(Exception e){
      return null;

IN MY MAIN CLASS

connect c = new connect(user,pass);

Upvotes: 1

Views: 8195

Answers (3)

Learn More
Learn More

Reputation: 1561

public class Connect
{
private static Connection sharedConnection;
public static Connection createOrAccessConnection(String user,String pass, boolean forceNew){ 
Connection connect = sharedConnection;
try
  {

      Class.forName("com.mysql.jdbc.Driver");
      if(forceNew || connect == null)
         { 
              connect = DriverManager.getConnection(
                "jdbc:mysql://localhost/MYDATABASE?user="+username+"&password="+password);
              if(sharedConnection == null ) 
                { 
                sharedConnection = connect; 
                }  
         }
return connect;
  }catch(Exception e){
      return null;
}
}



class Main
   {
   public void transactionMethod()
   {
       //Some code here.
       //This is what you need to add.
       Connection con = Connect.createConnection(user,pass,false);
       Sysout(con);
       AnotherClass ac = new AnotherClass();
       ac.operation1(con, false);
       ac.operation1(null,true);  
   }



class AnotherClass
       {
       public void operation1(Connection con, boolean createNew) 
            {
            if(createNew || con==null)
               {
               con = Connect.createConnection(user,pass,false);
               }
            //Some code here
            if(createNew)
               {
               //Close connection here. 
               }      
            }
       } 

       }

Upvotes: 1

Learn More
Learn More

Reputation: 1561

  • I can tell that you are not creating connection in Constructor because of return statement.

Please have a look below:

class Connect
{
public Connection createConnection(){ 
try
  {
      Class.forName("com.mysql.jdbc.Driver");
      connect = DriverManager.getConnection(
                "jdbc:mysql://localhost/MYDATABASE?user="+username+"&password="+password);
return connect;
  }catch(Exception e){
      return null;
}
}



class Main
   {
   public void connectionNeededHere()
   {
       //Some code here.
       ...
       //This is what your are calling. 
       Connect c = new connect(user,pass);
       //This is what you need to add.
       Connection con = c.createConnection();
       Sysout(con);
   }

   }

Upvotes: 1

Eric
Eric

Reputation: 1297

I'm guessing you've already done it correctly, but it's simply a case of using the method, there is no trick to it.

class A
{
    Connection connection;

    public void setConnection( Connection connection )
    {
        if ( connection == null ) System.out.println("null passed!");
        this.connection = connection;
    }
}

Now you simply call the method, that's it:

Connection c = null;
// TODO: added code here to establish the connection.
A a = new A();
if ( c == null ) System.out.println("c is null before passing it on!");
a.setConnection(c);

If the value being passed is null (not connected), then you need to look at the code that creates the connection, to make sure you're passing a valid (connected) object through.

EDIT
I just saw the code snippet that you added. If that try {...} catch is within your constructor, then it won't work ... shouldn't even compile! The constructor can not be used to return a value, only methods can.

Upvotes: 1

Related Questions