測試的tomcat為apache
配置步驟如下
<resource
<description>DB Connection</description>
<res
<res
<res
</resource
<resource
<description>DB Connection</description>
<res
<res
<res
</resource
<Context path=
<Resource name=
type=
username=
password=
driverClassName=
url=
maxIdle=
maxWait=
maxActive=
<parameter>
<name>removeAbandoned</name>
<value>true</value>
</parameter>
</Resource>
<Resource name=
type=
username=
password=
driverClassName=
url=
maxIdle=
maxWait=
maxActive=
<parameter>
<name>removeAbandoned</name>
<value>true</value>
</parameter>
</Resource>
</Context>
</Host>
或者
<Context path=
<Resource name=
type=
username=
password=
driverClassName=
url=
maxIdle=
maxWait=
maxActive=
</Resource>
</Context>
</Host>
注意
MysqlConn類
package com
import java
import java
import javax
import javax
import javax
public final class MysqlConn {
// 懶漢式單例(使用時才new)
private static MysqlConn instance = null;
MysqlConn() {
}
// 延遲初始化(用到的時候才加載)(推薦)
// public static synchronized JdbcConn
// getInstance(){}
public static MysqlConn getInstance() {
if (instance == null) {
synchronized (MysqlConn
if (instance == null) {
instance = new MysqlConn();
}
}
}
return instance;
}
private DataSource getDataSource() {
DataSource ds = null;
try {
Context ctx = new InitialContext();
ds = (DataSource) ctx
} catch (Exception e) {
System
e
}
return ds;
}
public Connection getConn() {
Connection conn = null;
try {
conn = getDataSource()
} catch (SQLException e) {
System
e
}
return conn;
}
}
OraclelConn類
package com
import java
import java
import javax
import javax
import javax
public final class OracleConn {
// 懶漢式單例(使用時才new)
private static OracleConn instance = null;
OracleConn() {
}
// 延遲初始化(用到的時候才加載)(推薦)
// public static synchronized JdbcConn
// getInstance(){}
public static OracleConn getInstance() {
if (instance == null) {
synchronized (OracleConn
if (instance == null) {
instance = new OracleConn();
}
}
}
return instance;
}
private DataSource getDataSource() {
DataSource ds = null;
try {
Context ctx = new InitialContext();
ds = (DataSource) ctx
} catch (Exception e) {
System
e
}
return ds;
}
public Connection getConn() {
Connection conn = null;
try {
conn = getDataSource()
} catch (SQLException e) {
System
e
}
return conn;
}
}
頁面index
<body>
mysql連接對象為
oracle連接對象為
</body>
From:http://tw.wingwit.com/Article/program/Java/ky/201311/28581.html