tomcat6.0连接池配置 SQL 2000

1.在tomcat/conf/server.xml中的<host></host>中加入
<host>
[color=red]<Context path="/log4jTest" docBase="log4jTest" debug="5" reloadable="true"> [color=darkblue].....................log4jTest为你的web工程名称[/color]
<Resource name="jdbc/netrep2" auth="Container" type="javax.sql.DataSource"
maxActive="100" maxIdle="30" maxWait="10000"
username="dd" password="dd" driverClassName="com.microsoft.jdbc.sqlserver.SQLServerDriver"
url="jdbc:microsoft:sqlserver://localhost:1421;DatabaseName=netrep2"/>
</Context>[/color]
</host>
2.打开工程下的web.xml加入
<description>SQL2000 Test App</description>
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/netrep2</res-ref-name>//[color=darkblue]netrep2为数据库名称[/color]
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
3测试类
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;

/**
* @author minyang
*
*/
public class DBTest {
String foo = "Not Connected";
int bar = -1;

public void init() {
try {
Context ctx = new InitialContext();
if (ctx == null)
throw new Exception("Boom - No Context");
DataSource ds = (DataSource) ctx
.lookup("java:comp/env/jdbc/netrep2");
if (ds != null) {
Connection conn = ds.getConnection();
Connection conn2=ds.getConnection();
if (conn != null) {
foo = "Got Connection " + conn.toString();
Statement stmt = conn.createStatement();
ResultSet rst = stmt.executeQuery(" select * from map_zone ");
if (rst.next()) {
foo = rst.getString(3);

System.out.println(rst.getString(1));
System.out.println(rst.getString(2));
System.out.println(rst.getString(3));
System.out.println(rst.getString(4));
bar = 208;
}
conn.close();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}

public String getFoo() {
return foo;
}

public int getBar() {
return bar;
}
public static void main(String[] args) throws Exception {
DBTest tst = new DBTest();
tst.init();
}
}
4 jsp

<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>

<body>
<%
DBTest tst = new DBTest();
tst.init();
%>
<h2>Results</h2>
Foo <%= tst.getFoo() %><br>
Bar <%= tst.getBar() %>
</body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值