1. in your web app, create "context.xml" in META-INF folder:
context.xml
2. Add following lines to web.xml
3. And then you can use the datasource in your web app like:
Note: Above configuration is not fit for building block, it will throw following exception:
org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create JDBC driver of class '' for connect URL 'null'
context.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <Context path="">
- <Resource name="jdbc/testDB"
- auth="Container"
- type="javax.sql.DataSource"
- factory="org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory"
- driverClassName="com.mysql.jdbc.Driver"
- url="jdbc:mysql://XXX.XXX.XXX:3306/yourDB"
- username="XXX"
- password=""
- maxActive="20"
- />
- </Context>
- <resource-ref>
- <res-ref-name>jdbc/testDB</res-ref-name>
- <res-type>javax.sql.DataSource</res-type>
- <res-auth>Container</res-auth>
- </resource-ref>
3. And then you can use the datasource in your web app like:
- Context context = new InitialContext();
- DataSource ds = (DataSource) context
- .lookup("java:comp/env/jdbc/testDB");
- Connection conn = ds.getConnection();
- ......
- conn.close();
Note: Above configuration is not fit for building block, it will throw following exception:
org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create JDBC driver of class '' for connect URL 'null'