mysql的驱动包要放到tomcat5.5.20\common\lib目录下,如是是tomcat6.x直接放到Tomcat6\lib目录下
在tomcat5.5.20\conf\Catalina\localhost目录下新建一文件,文件名为:你的项目名称.xml
如果你用的是tomcat6.x conf没有下没有Catalina\localhost这个目录,那就直接创建这个目录再加这个文件就可以了
文件内容如下:
<!--
Context configuration file for the Tomcat Manager Web App
$Id: manager.xml 303123 2004-08-26 17:03:35Z remm $
-->
<Context docBase="D:\janson\workspace\tomcat5.5.20\webapps\rolcoe"
privileged="true" antiResourceLocking="false" antiJARLocking="false">
<!-- Link to the user database we will get roles from -->
<ResourceLink name="users" global="UserDatabase"
type="org.apache.catalina.UserDatabase"/>
<Resource
name="jdbc/MysqlPool"
type="javax.sql.DataSource"
maxActive="4"
maxIdle="2"
username="root"
maxWait="5000"
driverClassName="com.mysql.jdbc.Driver"
password="1111"
url="jdbc:mysql://localhost:3306/rolcoe"/>
</Context>
获取连接类如下:
package com.rolcoe.daoFactory;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
public class ConnectionManage {
public final static String DBJNDI = "java:comp/env/jdbc/MysqlPool";//MysqlPool是连接池名称,要和上面的配置文件 name="jdbc/MysqlPool"中的MysqlPool一致
private static Context Actx = null;
private static DataSource Ads = null;
public static void CloseConn(Connection conn){
try {
conn.close();
} catch (SQLException ex) {
}
}
public static Connection getDBConn() {
Connection Aconn = null;
if (Actx == null) {
try {
Actx = new InitialContext();
} catch (NamingException ex) {
Actx = null;
System.out.print("初始化连接池错误!No Context!!" + ex.getMessage());
}
}
if (Actx != null) {
try {
Ads = (DataSource) Actx.lookup(DBJNDI);
} catch (NamingException ex) {
System.out.print("查找数据库连接池错误!!" + ex.getMessage());
}
if (Ads != null) {
try {
Aconn = Ads.getConnection();
} catch (SQLException ex) {
System.out.print("创建数据库连接错误!!" + ex.getMessage());
}
}
}
return Aconn;
}
/* public void close() {
if (cn != null)
try {
cn.close();
} catch (SQLException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
}*/
public static void close_p(Connection con, PreparedStatement pstm) {
try {
if (con != null)
con.close();
if (pstm != null)
pstm.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void close_s(Connection con, Statement pstm) {
try {
if (con != null)
con.close();
if (pstm != null)
pstm.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void close_s(Connection con, Statement pstm, ResultSet rs) {
try {
if (rs != null)
rs.close();
if (pstm != null)
pstm.close();
if (con != null)
con.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void close_p(Connection con, PreparedStatement pstm,
ResultSet rs) {
try {
if (rs != null)
rs.close();
if (pstm != null)
pstm.close();
if (con != null)
con.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String args[]) {
new ConnectionManage();
}
}