CSDN博客

img cooperking

Tomcat5+SQLServer连接池的配置方法

发表于2004/10/26 11:21:00  1389人阅读

进入Tomcat的admin管理界面,单击datasource配置你的参数,配置后的参数如下(jdbc的驱动为jtds):
JNDI Name: jdbc/mysql
Data Source URL: jdbc:jtds:sqlserver://localhost:1433/test
JDBC Driver Class: net.sourceforge.jtds.jdbc.Driver
User Name:test
Password:test
Max. Active Connections:4
Max. Idle Connections:2
Max. Wait for Connection:5000
Validation Query ://不添

在%TOMCAT_HOME%/conf/Catalina/localhost下找到你的web应用对应的.xml文件,如test.xml,并在此文件的下添入代码:

<ResourceLink name="jdbc/mysql" global="jdbc/mysql" type="javax.sql.DataSourcer"/>


至此配置成功,可用如下代码测试:

<%@ page import="java.sql.*"%>
<%@ page import="javax.naming.*"%>
<%@ page import="javax.sql.*"%>
<%
try{
Context initCtx = new InitialContext();
Context ctx = (Context) initCtx.lookup("java:comp/env");?
DataSource ds = (DataSource) ctx.lookup("jdbc/mysql");
Connection conn = ds.getConnection();
Statement stmt = conn.createStatement();
String strSql = " select * from userinfo";
ResultSet rs = stmt.executeQuery(strSql);
while(rs.next()){
out.println(rs.getString(1));?
}
}
catch(Exception ex){
ex.printStackTrace();
}
%>

阅读全文
0 0

相关文章推荐

img
取 消
img