一、Tomcat6.0中配置数据源
1.在Tomcat根目录/conf/Catalina/localhost目录下新建一个XML文件,文件名称跟工程名称一致.文件中的内容如下:
<?xml version='1.0' encoding='utf-8'?>
<Context docBase="E:\Eclipse3.4.2\workspace\mmis\WebContent" path="/mmis">
<Resource name="mmisds" type="javax.sql.DataSource"
factory="org.apache.commons.dbcp.BasicDataSourceFactory"
driverClassName="oracle.jdbc.driver.OracleDriver"
url="jdbc:oracle:thin:@192.168.2.250:1521:hmisb"
username="mtms" password="mtms"
validationQuery="select 1 from dual"
maxIdle="100" maxActive="500" maxWait="1000" defaultAutoCommit="true"
removeAbandoned="ture" removeAbandonedTimeout="60" logAbandoned="true"/>
</Context>
2.在tomcat的conf/context.xml中的<Context>标签中添加一个<Resource/>,内容如下:
<Resource name="jdbc/oa" auth="Container" type="javax.sql.DataSource"
factory="org.apache.commons.dbcp.BasicDataSourceFactory"
maxActive="100" maxIdle="500" maxWait="10000"
username="oa" password="oa" driverClassName="oracle.jdbc.driver.OracleDriver"
url="jdbc:oracle:thin:@172.16.60.30:1521:HMIS" defaultAutoCommit="true"
removeAbandoned="ture" removeAbandonedTimeout="60" logAbandoned="true" />
然后在web.xml<web-app>中添加引用(tomcat5.5以上可以不用添加这一段)
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/oa</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
2.1.获取连接对象
public class ConnectionPool{
public static Connection getConn()throws Exception{
//初始化上下文
Context initContext=getInitContext();
Context envContext=(Context)initContext.lookup("java:/comp/env");
DataSource dataSource=(DataSource)envContext.lookup("jdbc/oa");
//获取连接对象
return ds.getConnection();
}
}
docBase是指Web工程所在的位置,path是工程的名称, name是指JNDI的名称,type是数据源的类型,driverClassName是驱动名称,url是驱动的连接字符串
username是指数据库的用户名,password是指数据库的密码,defaultAutoCommit是否自动提交
二、Tomcat5.x中配置全局连接池
1.打Tomcat根目录/conf/server.xml文件,在<GlobalNamingResources>标签下添加
<GlobalNamingResources>
<Resource auth="Container" name="RLDMDataSource" scope="Shareable" type="javax.sql.DataSource" />
<ResourceParams name="RLDMDataSource">
<parameter>
<name>factory</name>
<value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
</parameter>
<parameter>
<name>driverClassName</name>
<value>oracle.jdbc.driver.OracleDriver</value>
</parameter>
<parameter>
<name>url</name>
<!--<value>jdbc:jtds:sqlserver: SQL数据库 -->
<value>jdbc:oracle:thin:@172.16.61.13:1521:服务名</value> <!-- Oracle数据库 -->
</parameter>
<parameter>
<name>username</name>
<value>clgc</value>
</parameter>
<parameter>
<name>password</name>
<value>clgc</value>
</parameter>
<parameter>
<name>validationQuery</name>
<value>select count(*) from dual</value>
</parameter>
<parameter>
<name>maxIdle</name>
<value>5</value>
</parameter>
<parameter>
<name>maxActive</name>
<value>10</value>
</parameter>
<parameter>
<name>maxWait</name>
<value>-1</value>
</parameter>
<parameter>
<name>removeAbandoned</name>
<value>true</value>
</parameter>
<parameter>
<name>removeAbandonedTimeout</name>
<value>60</value>
</parameter>
<parameter>
<name>logAbandoned</name>
<value>true</value>
</parameter>
</ResourceParams>
</GlobalNamingResources>
2.在Host标签下添加<Context>标签
<Host name="localhost" debug="0" appBase="webapps"
unpackWARs="true" autoDeploy="true"
xmlValidation="false" xmlNamespaceAware="false">
<Context path="/rldmpda" docBase="D:\Tomcat 5.0\webapps\rldmpda" debug="0" reloadable="true">
<ResourceLink name="jdbc/RldmDB" global="RLDMDataSource" type="javax.sql.DataSource" />
</Context>
</Host>
这里的<Context path="/rldmpda" docBase="D:\Tomcat 5.0\webapps\rldmpda" debug="0" reloadable="true">
path是指项目发布在tomcat webapps目录下的路径,我的项目目录为:../tomcat5.0/webapps/rldmpda
docBase是指项目的
在JSP里有两种实现的办法,一种是用JNDI(Java Naming Directory Interface),这可能和应用服务器有关,如果是Resin,先在resin.conf里定义
<resource-ref>
<res-ref-name>jdbc/oracle</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<init-param driver-name="oracle.jdbc.driver.OracleDriver"/>
<init-param url="jdbc:oracle:thin:@192.168.1.1:1521:oracle"/>
<init-param user="system"/>
<init-param password="manager"/>
<init-param max-connections="20"/>
<init-param max-idle-time="30"/>
</resource-ref>
如果为Tomcat,在Server.xml里面定义,有关的资料可以查文档,然后在jsp里这样用
try{
javax.naming.Context env = (Context)new InitialContext().lookup("java:comp/env");
javax.sql.DataSource pool=(javax.sql.DataSource) env.lookup("jdbc/oracle");
}catch(Exception e){System.err.println("Exception error:"+e.getMessage());}
try {
Connection conn = pool.getConnection();
}catch(Exception e){System.out.println("Exception error:"+e.getMessage());}
通过这段代码,你就获得从连接池里获得了一个连接conn。如果想用普通的连接池,那只能用JavaBean了,先写一个ConnectionPool的java的类,然后直接从连接池中获得连接,下面是我一个连接池的JavaBean
ConnectionPool.java如下:
import java.io.PrintStream;
import java.sql.Connection;
import java.util.Vector;
// Referenced classes of package com.ilovejsp.sql:
// DataSource, PooledConnection
public class ConnectionPool
{
private Vector pool;
private int size;
DataSource db;
public ConnectionPool()
{
pool = null;
size = 0;
db = new DataSource();
}
public void setSize(int value)
{
if(value > 1)
size = value;
}
public int getSize()
{
return size;
}
public synchronized void initPool()
throws Exception
{
try
{
for(int x = 0; x < size; x++)
{
Connection conn = db.getConnection();
if(conn != null)
{
PooledConnection pcon = new PooledConnection(conn);
addConnection(pcon);
}
}
}
catch(Exception e)
{
System.err.println(e.getMessage());
}
}
private void addConnection(PooledConnection pcon)
{
if(pool == null)
pool = new Vector(size);
pool.addElement(pcon);
}
public synchronized void releaseConnection(Connection conn)
{
int x = 0;
do
{
if(x >= pool.size())
break;
PooledConnection pcon = (PooledConnection)pool.elementAt(x);
if(pcon.getConnection() == conn)
{
System.err.println("Release Connection".concat(String.valueOf(String.valueOf(x))));
pcon.setInUse(false);
break;
}
x++;
}
while(true);
}
public synchronized Connection getConnection()
throws Exception
{
PooledConnection pcon = null;
for(int x = 0; x < pool.size(); x++)
{
pcon = (PooledConnection)pool.elementAt(x);
if(!pcon.inUse())
{
pcon.setInUse(true);
return pcon.getConnection();
}
}
try
{
Connection conn = db.getConnection();
pcon = new PooledConnection(conn);
pcon.setInUse(true);
pool.addElement(pcon);
}
catch(Exception e)
{
System.err.println("Exception error:".concat(String.valueOf(String.valueOf(e.getMessage()))));
}
return pcon.getConnection();
}
public synchronized void emptyPool()
{
for(int x = 0; x < pool.size(); x++)
{
System.err.println("Closing Jdbc Connection".concat(String.valueOf(String.valueOf(x))));
PooledConnection pcon = (PooledConnection)pool.elementAt(x);
if(!pcon.inUse())
{
pcon.close();
continue;
}
try
{
Thread.sleep(3000L);
pcon.close();
}
catch(Exception e)
{
System.out.println("Exception :".concat(String.valueOf(String.valueOf(e.getMessage()))));
}
}
db.close();
}
}
testpool.jsp内容如下:
<%@ page language="java" contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*"%>
<HTML>
<HEAD>
<TITLE>系统数据信息</TITLE>
</HEAD>
<BODY>
<%ConnectionPool db=new ConnectionPool();
Connection conn=db.getConnection();
Statement stmt=conn.createStatement();
String sql1="select * from pg_database ";
ResultSet rs=stmt.executeQuery(sql1);
%>
<TABLE><TR><TD>系统数据库信息</TD></TR>
<TR><TD>
<%while(rs.next()) {
%>
<%=rs.getString(1)%>
<%}
rs.close();%>
</TR></TD>
<TABLE><TR><TD>系统字段信息</TD></TR>
<TR><TD>
<%String sql2="select * from pg_type";
rs=stmt.executeQuery(sql2);
while(rs.next()) {
%>
(<%=rs.getString(1)%>)
<%}
rs.close();
db.close();%>
</TR></TD>
</BODY>
</HTML>