tomcat6.0连接池配置 SQL 2000

1.在tomcat/conf/server.xml中的<host></host>中加入
<host>
<Context path="/log4jTest" docBase="log4jTest" debug="5"  reloadable="true">  [color=darkblue].....................log4jTest为你的web工程名称
      <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>// netrep2为数据库名称
<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>

你可能感兴趣的:(java,sql,jsp,jdbc,SQL Server)