Guest User

Untitled

a guest
Mar 20th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.99 KB | None | 0 0
  1. String initialContext = "java:comp/env";
  2. Context env = (Context) new InitialContext().lookup(initialContext);
  3. Object o = env.lookup(name);
  4.  
  5. Use this code in try block
  6. Context initContext = new InitialContext();
  7. Context envContext = (Context) initContext.lookup("java:comp/env");
  8. DataSource ds = (DataSource) envContext.lookup("jdbc/UsersDB");
  9. Connection connection = ds.getConnection();
  10. **add this on context. xml**
  11. <Resource
  12. name="jdbc/UsersDB"
  13. auth="Container"
  14. type="javax.sql.DataSource"
  15. maxActive="100"
  16. maxIdle="30"
  17. maxWait="10000"
  18. driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver"
  19. url="jdbc:sqlserver://192.168.1.56:1433;DatabaseName=testdb1;"
  20. username="sa"
  21. password="sa@sql"
  22. />
  23. **web.xml**
  24. <resource-ref>
  25. <description>DB Connection</description>
  26. <res-ref-name>jdbc/UsersDB</res-ref-name>
  27. <res-type>javax.sql.DataSource</res-type>
  28. <res-auth>Container</res-auth>
  29. </resource-ref>
Add Comment
Please, Sign In to add comment