How to store Connection object and call it from other programs.

Hi,

I am trying to connect to the database, store the connection object and use this connection object from other standalone java programs.

Can any one tell me how to do this? I've tried in the following way:

In the following program I am connecting to the database and saving the connection object in a variable.

publicclass GetKT2Connection{

publicstaticvoid main(String[] args){

String url ="jdbc:odbc:SQLDsn;

String dbUser ="sa";

String dbPwd ="sa";

Connection kt2conn = Connection connection = java.sql.DriverManager.getConnection(url, dbUser, dbPwd);

if(kt2conn ==null){

System.out.println("Database Connection Failure!");

}

else{

System.out.println("Connected to Database...");

}

GetKTConnectionObj.storeKT2ConnectionObj(kt2conn);

}

}

Here is the program to save connection object in a variable.

publicclass GetKTConnectionObj{

static Connection kt2Connection =null;

publicstaticvoid storeKT2ConnectionObj(Connection conn){

kt2Connection = conn;

}

publicstatic Connection getKT2ConnectionObj(){

try{

return kt2Connection;

}

catch(Exception e){

System.out.println(e);

}

returnnull;

}

Now from the following code I am trying to get the connection object that is stored. But this is throwing NullPointerException.

publicclass Metrics_Migration{

publicstaticvoid main(String args[]){

try{

java.sql.Connection connection_1 =GetKTConnectionObj.getKT6ConnectionObj();

}

catch(Exception e){

}

}

}

[3424 byte] By [krishna_123a] at [2007-11-27 2:21:11]
# 1
kt2Connection is null. You need to store it first, to make it not null. Otherwise it will stay null forever. And why on earth are you trying to do this THIS way?If you are running the two applications separately, it wont work either.
kmangolda at 2007-7-12 2:23:28 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...