您的位置:首页 > 运维架构 > Tomcat

Tomcat6.0中jdbc连接池配置

2009-06-27 13:37 507 查看
1、在context.xml中的context标签内加入以下代码:
<Resource name="jdbc/myoracle" auth="Container"
type="javax.sql.DataSource" driverClassName="oracle.jdbc.OracleDriver"
url="jdbc:oracle:thin:@127.0.0.1:1521:orclsid"
username="jike" password="jike" maxActive="20" maxIdle="10"
maxWait="-1"/>
2、在web.xml中web-app下加入以下代码
<resource-ref>
<description>Oracle Datasource example</description>
<res-ref-name>jdbc/myoracle</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
3、在javabean中引用

在网页后台处理程序(JavaBean)的代码中使用JNDI
包引入:
import java.sql.Connection;
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;

try {
Context envContext = (Context)new InitialContext().lookup("java:/comp/env");
DataSource ds = (DataSource)envContext.lookup("jdbc/myoracle"); //查找配置

Connection conn = ds.getConnection();
Statement st = conn.createStatement();
ResultSet rs = st.executeQuery("select id from pic");
out.println("dsdaffsdffd中");
while(rs.next()){
out.println(rs.getString("id") + "<br>");
}
} catch (NamingException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: