`
webdev2014
  • 浏览: 677344 次
文章分类
社区版块
存档分类
最新评论

连接池配置-tomcat

 
阅读更多

两种方式:
一、Tomcat服务器配置:
1、 把 <Resource name="jdbc/TestDB" auth="Container" type="javax.sql.DataSource"

maxActive="100" maxIdle="30" maxWait="10000"

username="root" password="sa" driverClassName="com.mysql.jdbc.Driver"

url="jdbc:mysql://localhost:3306/employee_manager?autoReconnect=true"/>

粘贴到Tomcat 6.0/conf context.xml 文件中的<Context></Context>

2、把 <resource-ref>

<description>DB Connection</description>

<res-ref-name>jdbc/TestDB</res-ref-name>

<res-type>javax.sql.DataSource</res-type>

<res-auth>Container</res-auth>

</resource-ref>

粘贴到web.xml 文件中的<web-app></ web-app >

3、把数据库驱动文件放在Tomcat 6.0/lib 中

4、程序中应用:

public static Connection getConnection() {

DataSource ds;

InitialContext cxt ;

try{

cxt = new InitialContext();

ds = (DataSource) cxt.lookup( "java:/comp/env/jdbc/TestDB" );

con = ds.getConnection();

}catch(Exception e){

e.printStackTrace();

}

SqlServer2000和以上步骤一样:但注意

url="jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=employee_manager"

?autoReconnect=true 应该删除!


二、在工程中配置:
1、在WebRoot/META-INF中创建一个context.xml文件,内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<Context>
<Resource name="jdbc/ORACLE"
auth="Container"
type="javax.sql.DataSource"
driverClassName="oracle.jdbc.driver.OracleDriver"
url="jdbc:oracle:thin:@127.0.0.1:1521:orcl"
username="scott"
password="tiger"
maxActive="100"
maxIdle="30"
maxWait="10000" />
</Context>
2、使用代码如下:
try{
InitialContext initContext = new InitialContext();
Context context = (Context)initContext.lookup("java:comp/env");
DataSource ds = (DataSource) context.lookup("jdbc/ORACLE");

conn = ds.getConnection();
System.out.println("通过连接池连接到数据库。");
}catch(Exception e){
throw new ServletException(e);
}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics