1

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);
7
  • 1
    Show us the code so we can help you better (besides, passing a connection to another object is ugly) Commented Jul 29, 2013 at 11:59
  • Show us some code , where and how you are passing . You can have a ConnectionManager class and other class can request Connection object from that class. Commented Jul 29, 2013 at 11:59
  • 1
    If the receiving class gets a null, then you're passing it a null. The parameters can't magically become null by themselves. Commented Jul 29, 2013 at 12:01
  • @Kayaman I have no idea. That is what actually happened. I tried executing a query inide my first class and it worked. In my second class the same query throws a null pointer exception ! Commented Jul 29, 2013 at 12:07
  • 1
    @XperiazX catch(Exception e){ return null; } is what's happening. Add e.printStackTrace(); and see for yourself. Commented Jul 29, 2013 at 12:11

3 Answers 3

1
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. 
               }      
            }
       } 

       }
Sign up to request clarification or add additional context in comments.

1 Comment

Above code should solve your problem. Also, note that it facilitates creation of connection at static load time and dynamically as well.
1

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.

1 Comment

This really did help me. My problem was that a Connection, as a member in my main class (public or private) could never be passed to other objects. (maybe because it was static but still have no clue why) But a locally created non static Connection could be easily passed and received.
1
  • 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);
   }

   }

2 Comments

class, not Class for both Connect and Main ;)
Thanks a lot for the answer. But my problem was passing data member of main class to another class.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.