2007-04-17
配置tomcat 6.0.10连接池
作者:飞阳
转载请注明出处:http://hi.baidu.com/burtcn
在工大后院问了很久的一个问题,http://www.gdutbbs.com/viewthread.php?tid=154093
版主回复我的时候,我就打算再自己试一下,不行的话再参照他的代码。结果真的可以。
参照了官方的说明文档:
http://tomcat.apache.org/tomcat-6.0-doc/jndi-datasource-examples-howto.html
下面以mysql数据库为例:
首先设置好数据库:
登录用户名为:javauser
登录密码为:javadude
命令:grant all privileges on *.* to javauser@localhost identified by "javadude";
数据库:javatest
数据表:testdata
命令:
create database javatest;
create table testdata(id int not null auto_increment primary key,foo varchar(25),bar int);
insert into testdata values(null,'hellojava',1011);
配置web.xml文件(位于:$CATALINA_HOME/conf/server.xml)
在<host></host>中添加下列代码:
同理,在配置好sql server之后,可以设置为
修改web应用的目录WEB-INF的web.xml文件。
在<web-app></web-app>中添加下列代码:
再下载JSTL,方便操作数据库
http://people.apache.org/builds/jakarta-taglibs/nightly/projects/standard/
把jstl.jar standart.jar导入到eclipse的WEB-INF/lib目录下
然后写一个JSP文件就可以了:
也可以不用JSTL,自己写一个类Conn.java:
在JSP中调用这个类:
转载请注明出处:http://hi.baidu.com/burtcn
在工大后院问了很久的一个问题,http://www.gdutbbs.com/viewthread.php?tid=154093
版主回复我的时候,我就打算再自己试一下,不行的话再参照他的代码。结果真的可以。
参照了官方的说明文档:
http://tomcat.apache.org/tomcat-6.0-doc/jndi-datasource-examples-howto.html
下面以mysql数据库为例:
首先设置好数据库:
登录用户名为:javauser
登录密码为:javadude
命令:grant all privileges on *.* to javauser@localhost identified by "javadude";
数据库:javatest
数据表:testdata
命令:
create database javatest;
create table testdata(id int not null auto_increment primary key,foo varchar(25),bar int);
insert into testdata values(null,'hellojava',1011);
配置web.xml文件(位于:$CATALINA_HOME/conf/server.xml)
在<host></host>中添加下列代码:
<Context path="/DBTest" docBase="DBTest"
debug="5" reloadable="true" crossContext="true">
<Resource name="jdbc/TestDB" auth="Container" type="javax.sql.DataSource"
maxAtive="100" maxIdle="30" maxWait="10000"
username="javauser" password="javadude" driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/javatest?autoReconnect=true" />
</Context>
同理,在配置好sql server之后,可以设置为
<Context path="/DBTest" docBase="DBTest"
debug="5" reloadable="true" crossContext="true">
<Resource name="jdbc/TestDB" auth="Container" type="javax.sql.DataSource"
maxAtive="100" maxIdle="30" maxWait="10000"
username="sa" password="sa" driverClassName="com.microsoft.jdbc.sqlserver.SQLServerDriver"
url="jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=javatest" />
</Context>
修改web应用的目录WEB-INF的web.xml文件。
在<web-app></web-app>中添加下列代码:
<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>
再下载JSTL,方便操作数据库
http://people.apache.org/builds/jakarta-taglibs/nightly/projects/standard/
把jstl.jar standart.jar导入到eclipse的WEB-INF/lib目录下
然后写一个JSP文件就可以了:
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<sql:query var="rs" dataSource="jdbc/TestDB">
select id, foo, bar from testdata
</sql:query>
<html>
<head>
<title>DB Test</title>
</head>
<body>
<h2>Results</h2>
<c:forEach var="row" items="${rs.rows}">
Foo ${row.foo}<br/>
Bar ${row.bar}<br/>
</c:forEach>
</body>
</html>
也可以不用JSTL,自己写一个类Conn.java:
package cn.burt.connpool;
import java.sql.*;
import javax.naming.*;
import javax.sql.*;
public class Conn {
public static synchronized Connection get() throws Exception {
Context ctx = new InitialContext();
Context envctx = (Context) ctx.lookup("java:comp/env");
DataSource ds = (DataSource) envctx.lookup("jdbc/TestDB");
return ds.getConnection();
}
}
在JSP中调用这个类:
<%@ page language="java" import="java.util.*,cn.burt.connpool.*" pageEncoding="gb2312"%>
<%@ page import="java.sql.*,javax.sql.*" %>
<%
Connection conn=null;
Statement st=null;
ResultSet rs=null;
try{
conn=get();
st=conn.createStatement();
rs=st.executeQuery("select * from testdata");
while(rs.next()){
System.out.println(rs.getString("foo"));
}
st.close();
rs.close();
conn.close();
}catch(Exception e){
e.printStackTrace();
}
%>







评论排行榜