Advertisement
Guest User

Untitled

a guest
Sep 18th, 2017
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
XML 1.01 KB | None | 0 0
  1. <!-- inside server.xml -->
  2. <Context path="/myApp">
  3.         <Resource name="jdbc/testdb" auth="Container"
  4.                 type="javax.sql.DataSource" driverClassName="org.postgresql.Driver"
  5.                 url="jdbc:postgresql://localhost:5432/testdb"
  6.                 username="postgres" password="" maxActive="20" maxIdle="10" maxWait="-1"
  7.         />
  8. </Context>
  9.  
  10.  
  11. <!-- inside web.xml -->
  12. <resource-ref>
  13.  <description>postgreSQL Datasource example</description>
  14.  <res-ref-name>jdbc/testdb</res-ref-name>
  15.  <res-type>javax.sql.DataSource</res-type>
  16.  <res-auth>Container</res-auth>
  17. </resource-ref>
  18.  
  19.  
  20.  
  21. /* The JSP code */
  22. <%@ page import="java.sql.*, javax.sql.*, javax.naming.*"%>
  23. <% 
  24. InitialContext cxt = new InitialContext();
  25. if ( cxt == null ) {
  26.   throw new Exception("Uh oh -- no context!");
  27. }
  28.  
  29. DataSource ds = (DataSource) cxt.lookup( "java:/comp/env/jdbc/testdb" );
  30. if ( ds == null ) {
  31.   throw new Exception("Data source not found!");
  32. }
  33. try {
  34.     Connection con = ds.getConnection();
  35. } catch (Exception e) {
  36.     out.println(e.getMessage());
  37. }
  38. %>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement