只需在tomcat\webapps\mypreject\META-INF\context.xml的Context中增加:
<Context
path="/mywebapp"
docBase="mywebapp"
debug="5"
reloadable="true"
crossContext="true" >
<Resource name="jdbc/mysqlds"
auth="Container"
type="javax.sql.DataSource"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/spms?autoReconnect=true"
username="root"
password="test"
maxActive="100"
maxIdle="30"
maxWait="10000"
/>
</Context>
参数说明:
name:
driveClassName:JDBC驱动类的完整的名称;
maxActive:同时能够从连接池中被分配的可用实例的最大数;
maxIdle:可以同时闲置在连接池中的连接的最大数;
maxWait:最大超时时间,以毫秒计;
password:用户密码;
url:到JDBC的URL连接;
user:用户名称;
测试:
//连接池的获取
Connection conn = null;
DataSource ds = null;
ResultSet rs =null;
Statement stmt = null;
Context initCtx = new InitialContext();
ds =(DataSource)initCtx.lookup("java:comp/env/jdbc/mysqlds");
if(ds!=null){
conn = ds.getConnection();
try{
stmt = conn.createStatement();
String sql ="select * from tree_table";
rs = stmt.executeQuery(sql);
while(rs.next()){
}
} catch(Exception ex){
ex.printStackTrace();
}finally{
conn.close();
rs.close();
stmt.close();
}
}