F
francan00
I would like to create a single Database connection point that I can
use for 4 classes in my Java Web Application.
Here is my ConnectionManager Class:
public class ConnectionManager {
private static Connection activeConnection = null;
public static Connection getConnection() {
if (activeConnection = null) {
Class.forName("OracleThinInfoHere...");
activeConnection =
DriverManager.getConnection("jdbcracle:thinmyname:1234rcl",
"scott", "tiger");
);
}
return activeConnection;
}
}
Now how would I access this in each one of my classes?
For example here is one:
public class MainClass
{
public ConnectionManager.getConnection(),
public Connection connection;
//I tried my db connection as this and it didnt return any results
public MainClass(connection)
{
this.connection = ConnectionManager.getConnection();
}
public int matcher(BeanClass abc)
{
try
{
new OtherDbClass(connection).insertDbMethod(abc);
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
//closing statements here
}
OtherClass looks like this:
public class OtherClass {
private Connection connection;
public OtherClass(Connection connection)
{
this.connection = ConnectionManager.getConnection();
}
public int insertDbMethod(BeanClass abc)
{
....
}
Please advise.
use for 4 classes in my Java Web Application.
Here is my ConnectionManager Class:
public class ConnectionManager {
private static Connection activeConnection = null;
public static Connection getConnection() {
if (activeConnection = null) {
Class.forName("OracleThinInfoHere...");
activeConnection =
DriverManager.getConnection("jdbcracle:thinmyname:1234rcl",
"scott", "tiger");
);
}
return activeConnection;
}
}
Now how would I access this in each one of my classes?
For example here is one:
public class MainClass
{
public ConnectionManager.getConnection(),
public Connection connection;
//I tried my db connection as this and it didnt return any results
public MainClass(connection)
{
this.connection = ConnectionManager.getConnection();
}
public int matcher(BeanClass abc)
{
try
{
new OtherDbClass(connection).insertDbMethod(abc);
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
//closing statements here
}
OtherClass looks like this:
public class OtherClass {
private Connection connection;
public OtherClass(Connection connection)
{
this.connection = ConnectionManager.getConnection();
}
public int insertDbMethod(BeanClass abc)
{
....
}
Please advise.