server.xml
<Resource name="jdbc/asdf" auth="Container" type="javax.sql.DataSource" maxActive="100" maxIdle="30" maxWait="10000"
username="asdf" password="asdf" driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://192.168.0.1:3306/asdf?autoReconnect=true"/>
<Host appBase="webapps" autoDeploy="true" name="localhost.com" unpackWARs="true" xmlNamespaceAware="false" xmlValidation="false">
<Context docBase="www" path="" reloadable="true" >
<ResourceLink global="jdbc/asdf" name="jdbc/asdf" type="com.mysql.jdbc.Driver"/>
</Context>
</Host>
db.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<%@ page import="javax.sql.*" %>
<%@ page import="javax.naming.*" %>
<Resource name="jdbc/asdf" auth="Container" type="javax.sql.DataSource" maxActive="100" maxIdle="30" maxWait="10000"
username="asdf" password="asdf" driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://192.168.0.1:3306/asdf?autoReconnect=true"/>
<Host appBase="webapps" autoDeploy="true" name="localhost.com" unpackWARs="true" xmlNamespaceAware="false" xmlValidation="false">
<Context docBase="www" path="" reloadable="true" >
<ResourceLink global="jdbc/asdf" name="jdbc/asdf" type="com.mysql.jdbc.Driver"/>
</Context>
</Host>
db.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<%@ page import="javax.sql.*" %>
<%@ page import="javax.naming.*" %>
<%
String foo = "Not Connected";
int bar = -1;
try {
Context ctx = new InitialContext();
if (ctx == null)
throw new Exception("Boom - No Context");
DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc/asdf");
if (ds != null) {
Connection conn = ds.getConnection();
if (conn != null) {
foo = "Got Connection " + conn.toString();
Statement stmt = conn.createStatement();
ResultSet rst = stmt.executeQuery("select a from test");
if (rst.next()) {
foo = rst.getString(1);
}
conn.close();
}
}
out.println("curdate() : " + foo);
} catch (Exception e) {
e.printStackTrace();
}
%>