<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/testpool</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
<res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref> |
import javax.naming.InitialContext;
import javax.sql.DataSource;
InitialContext ctx=new InitialContext();
DataSource dataSource=(DataSource)ctx.lookup("java:comp/env/jdbc/testpool");
Connection con=dataSource.getConnection(); |
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<%@ page import="java.sql.*"%>
<%@ page import="javax.sql.*"%>
<%@ page import="javax.naming.*"%>
<%@ page session="false"%>
<!DOCTYPE HTML PUBLIC "-//W
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>test for DB connection</title>
<%
out.print("beginning testing...");
out.print("<br>");
DataSource ds = null;
try {
InitialContext ctx = new InitialContext();
ds = (DataSource) ctx.lookup("java:comp/env/jdbc/testpool");
Connection conn = ds.getConnection();
Statement stmt = conn.createStatement();
// test是数据库已有的表,
//这里的数据库是前文提及的Data Source URL配置里包含的数据库。
String strSql = " select * from test";
ResultSet rs = stmt.executeQuery(strSql);
while (rs.next()) {
out.print(rs.getString(1));
}
out.print("<br>");
out.print("end testing.");
} catch (Exception ex) {
out.print("error:" + ex.getMessage());
ex.printStackTrace();
}
%>
</head>
<body>
</body>
</html> |
7.查询配置文件
<?xml version='1.0' encoding='utf-8'?>
<Context docBase="ConnectorPool" path="/ConnectorPool"
workDir="work\Catalina\localhost\ConnectorPool">
<Resource auth="Container" description="DB Connection" name="jdbc/testpool" type="javax.sql.DataSource"/>
<ResourceParams name="jdbc/testpool">
<parameter>
<name>maxWait</name>
<value>5000</value>
</parameter>
<parameter>
<name>maxActive</name>
<value>4</value>
</parameter>
<parameter>
<name>password</name>
<value>root</value>
</parameter>
<parameter>
<name>url</name>
<value>jdbc:mysql://localhost:3306/conpool</value>
</parameter>
<parameter>
<name>driverClassName</name>
<value>com.mysql.jdbc.Driver</value>
</parameter>
<parameter>
<name>maxIdle</name>
<value>2</value>
</parameter>
<parameter>
<name>username</name>
<value>root</value>
</parameter>
</ResourceParams>
</Context> |