Using the following method you will get the connection in your Bean/Servlet for your requirement.
For this you just need one parameter from your Server that is the DataSource Name.
use the following code in your Bean/Servlet
import javax.naming.Context;
import javax.naming.InitialContext;
private Connection getDBConnection() {
Context ctx;
Connection conn = null;
try {
ctx = new InitialContext();
DataSource ds;
ds = (DataSource)ctx.lookup("java:comp/env/jdbc/xyzDS");
conn = ds.getConnection();
} catch (Exception e) {
// TODO: Add catch code
e.printStackTrace();
}
return conn;
}
xyzDS -> is the DataSource Name.
For this you just need one parameter from your Server that is the DataSource Name.
use the following code in your Bean/Servlet
import javax.naming.Context;
import javax.naming.InitialContext;
private Connection getDBConnection() {
Context ctx;
Connection conn = null;
try {
ctx = new InitialContext();
DataSource ds;
ds = (DataSource)ctx.lookup("java:comp/env/jdbc/xyzDS");
conn = ds.getConnection();
} catch (Exception e) {
// TODO: Add catch code
e.printStackTrace();
}
return conn;
}
xyzDS -> is the DataSource Name.
Comments
Post a Comment