Guest User

Untitled

a guest
Feb 22nd, 2018
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.08 KB | None | 0 0
  1. <GlobalNamingResources>
  2. <Resource
  3. auth="Container"
  4. driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver"
  5. logAbandoned="true"
  6. maxActive="20"
  7. maxIdle="10"
  8. maxWait="5000"
  9. name="jdbc/datasource1"
  10. removeAbandoned="true"
  11. removeAbandonedTimeout="60"
  12. type="javax.sql.DataSource"
  13. url="jdbc:sqlserver://localhost;databaseName=database1"
  14. username="username"
  15. password="password"/>
  16. </GlobalNamingResources>
  17.  
  18. try {
  19. Context initCtx = new InitialContext();
  20. Context envCtx = (Context) initCtx.lookup("java:comp/env");
  21. DataSource ds = (DataSource) envCtx.lookup("datasource1");
  22. ds.getConnection(); // If this line doesn't throw SQLException, the db connection is established successfully
  23. } catch(NamingException ex) {
  24. System.out.println("JNDI lookup failed: " + ex.getMessage());
  25. } catch(SQLException se) {
  26. System.out.println("Unable to establish connection: " + se.getMessage());
  27. }
  28.  
  29. <ResourceLink global="jdbc/datasource1" name="datasource1" type="javax.sql.DataSource"/>
Add Comment
Please, Sign In to add comment