1. web.xml 配置文件:
<!-- <Context path="/test" docBase="D:/eclipseSDK213win32/eclipse/workspace/report/web" debug="5" reloadable="true" crossContext="true">
<Logger className="org.apache.catalina.logger.FileLogger" prefix="localhost_DBTest_log." suffix=".txt" timestamp="true"/>
<Resource name="jdbc/SqlServerDB" auth="Container" type="javax.sql.DataSource"/>
<ResourceParams name="jdbc/SqlServerDB">
<parameter>
<name>factory</name>
<value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
</parameter>
<!-- Maximum number of dB connections in pool. Make sure you configure your mysqld max_connections large enough to handle all of your db connections. Set to 0 for no limit.-->
<parameter>
<name>maxActive</name>
<value>50</value>
</parameter>
<!-- Maximum number of idle dB connections to retain in pool. Set to 0 for no limit.-->
<parameter>
<name>maxIdle</name>
<value>20</value>
</parameter>
<!-- Maximum time to wait for a dB connection to become available in ms, in this example 0.5 seconds. An Exception is thrown if this timeout is exceeded. Set to -1 to wait indefinitely. -->
<parameter>
<name>maxWait</name>
<value>500</value>
</parameter>
<!-- msSQL dB username and password for dB connections -->
<parameter>
<name>username</name>
<value>sa</value>
</parameter>
<parameter>
<name>password</name>
<value>123</value>
</parameter>
<!-- Class name for SQLServer2000 JDBC driver -->
<parameter>
<name>driverClassName</name>
<value>com.microsoft.jdbc.sqlserver.SQLServerDriver</value>
</parameter>
<!-- The JDBC connection url for connecting to your MS SQL Server dB.The autoReconnect=true argument to the url makes sure that the mm.Sql Server JDBC Driver will automatically reconnect if mysqld closed the connection. mysqld by default closes idle connections after 8 hours.-->
<parameter>
<name>url</name>
<value>jdbc:microsoft:sqlserver://localhost:1433;databaseName=meip2</value>
<!--must use & not use & -->
</parameter>
</ResourceParams>
</Context>
-->
2. 调用代码:
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;
import util.smartDateFormat;
public class dbManager {
/************************************
* @param static private boolean VERBOSE ;
* @param Statement theStatement;
* @param PreparedStatement thePstmt;
* @param Connection theConnection;
************************************/
final static private boolean VERBOSE = true; //???????
//static Logger logger = Logger.getLogger(dbManager.class.getName());
private Context initCtx = null;
private Context ctx = null;
private DataSource ds = null;
private long timeout = 5000;
private Statement theStatement = null;
private PreparedStatement thePstmt = null;
/************************************
* ???initCtx
* ???????
************************************/
public
dbManager() {
try {
initCtx = new InitialContext();
//init context,read config web.xml
if (initCtx == null) {
throw new Exception("Initial Failed!");
}
ctx = (Context) initCtx.lookup("java:comp/env");
//find "jdbc/SqlServerDB" object this configruation in the SERVER.XML of Tomcat
if (ctx != null) {
ds = (DataSource) ctx.lookup("jdbc/SqlServerDB");
}
if (ds == null) {
throw new Exception("Look up DataSource Failed!");
}
}
catch (Exception e) {
log(e, "Can't get the Context!");
}
}
/************************************
* get Connection
* @return Connection
************************************/
public synchronized
Connection getConnection() {
//get connection and set to delay time
long startTime = new java.util.Date().getTime();
Connection con = null;
while (con == null) {
con = newConnection();
if (con != null) {
//log("Create New Connection!");
break;
}
try {
log("????,????,??" + timeout + "ms");
wait(timeout);
}
catch (InterruptedException e) {
log(e, "????!");
}
if ( (new java.util.Date().getTime() - startTime) >= timeout) {
log("Connection timeout!");
break;
}
}
return con;
}
private
Connection newConnection() {
Connection con = null;
try {
con = ds.getConnection();
if (con == null) {
throw new Exception("Create Connection Failed!");
}
}
catch (Exception e) {
log("Create Connection Failed!");
System.out.println(e.getMessage());
}
return con;
}
/************************************
* release the connection
* @param conn Connection
* @param stmt Statement
* @param pstmt PreparedStatement
************************************/
public synchronized
void freeConnection(Connection conn,
Statement stmt,
PreparedStatement pstmt) {
try {
//close Statement
if (stmt != null) {
stmt.close();
stmt = null;
//log("Close Statement......");
}
//close PreparedStatement
if (pstmt != null) {
pstmt.close();
pstmt = null;
//log("Close PreparedStatement......");
}
}
catch (Exception e) {
System.out.println(e.getMessage());
}
try {
//close Connection
if (conn != null) {
conn.close();
conn = null;
//log("Close Connection......");
}
}
catch (SQLException e) {
log(e, "??????!");
}
}
/************************************
* write log file.
* @param s String
************************************/
private
void log(String s) {
if (VERBOSE) {
System.out.println(new java.util.Date() + ":" + s);
//logger.info(new java.util.Date()+s);
}
}
/************************************
* write log file.
* @param ex Object
************************************/
private
void logerr(Object ex) {
if (VERBOSE) {
//System.out.println(new java.util.Date()+":"+s);
//logger.error(ex);
}
}
/************************************
* write log file.
* @param e Throwable
* @param msg String
************************************/
private
void log(Throwable e, String msg) {
System.out.println(new java.util.Date() + ": " + msg);
//logger.info(new java.util.Date() + ": " + msg, e);
}
……
}