Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- String initialContext = "java:comp/env";
- Context env = (Context) new InitialContext().lookup(initialContext);
- Object o = env.lookup(name);
- Use this code in try block
- Context initContext = new InitialContext();
- Context envContext = (Context) initContext.lookup("java:comp/env");
- DataSource ds = (DataSource) envContext.lookup("jdbc/UsersDB");
- Connection connection = ds.getConnection();
- **add this on context. xml**
- <Resource
- name="jdbc/UsersDB"
- auth="Container"
- type="javax.sql.DataSource"
- maxActive="100"
- maxIdle="30"
- maxWait="10000"
- driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver"
- url="jdbc:sqlserver://192.168.1.56:1433;DatabaseName=testdb1;"
- username="sa"
- password="sa@sql"
- />
- **web.xml**
- <resource-ref>
- <description>DB Connection</description>
- <res-ref-name>jdbc/UsersDB</res-ref-name>
- <res-type>javax.sql.DataSource</res-type>
- <res-auth>Container</res-auth>
- </resource-ref>
Add Comment
Please, Sign In to add comment