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

tomcat下配置数据源

2006-04-15 15:46 591 查看
<Context path="/DBTest" docBase="DBTest"
debug="5" reloadable="true" crossContext="true">
<!—注意到这里的DBTest了吧,DBTest就是你的web工程,当你没有修改
/tomcat/conf/service.xml而删除了对应的web工程之后。tomcat将显示错误-->

<Logger className="org.apache.catalina.logger.FileLogger"
prefix="localhost_DBTest_log." suffix=".txt"
timestamp="true"/> //定义log位置

<Resource name="jdbc/TestDB"
auth="Container"
type="javax.sql.DataSource"/>
<ResourceParams name="jdbc/TestDB">//在程序里jndi使用的名称。
<parameter>
<name>factory</name>
<value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
</parameter>

<!-- Maximum number of dB connections in pool. Make sure you
configure your mysqld max_connections large enough to handle
all of your db connections. Set to 0 for no limit.
-->
<parameter>
<name>maxActive</name>
<value>100</value>
</parameter>

<!-- Maximum number of idle dB connections to retain in pool.
Set to 0 for no limit.
-->
<parameter>
<name>maxIdle</name>
<value>30</value>
</parameter>

<!-- Maximum time to wait for a dB connection to become available
in ms, in this example 10 seconds. An Exception is thrown if
this timeout is exceeded. Set to -1 to wait indefinitely.
-->
<parameter>
<name>maxWait</name>
<value>10000</value>
</parameter>

<!-- MySQL dB username and password for dB connections -->
<parameter>
<name>username</name>
<value>javauser</value>
<!—数据库用户名-->

</parameter>
<parameter>
<name>password</name>
<value>javadude</value>
<!—数据库密码-->
</parameter>
<!-- Class name for the old mm.mysql JDBC driver - uncomment this entry and comment next
if you want to use this driver - we recommend using Connector/J though
<parameter>
<name>driverClassName</name>
<value>org.gjt.mm.mysql.Driver</value>
</parameter>
这里面是被注释的,因为以前连接mysql是这样连接的,现在建议用下面的方法,自己可以看到。
-->

<!-- Class name for the official MySQL Connector/J driver -->
<parameter>
<name>driverClassName</name>
<value>com.mysql.jdbc.Driver</value>
</parameter>

<!-- The JDBC connection url for connecting to your MySQL dB.
The autoReconnect=true argument to the url makes sure that the
mm.mysql JDBC Driver will automatically reconnect if mysqld closed the
connection. mysqld by default closes idle connections after 8 hours.
-->
<parameter>
<name>url</name>
<value>jdbc:mysql://localhost:3306/javatest?autoReconnect=true</value>
<!--这是数据库的地址,也可以不要这么繁琐,改为
jdbc:mysql://localhost/javatest即可,javatest为数据库名-->
</parameter>
</ResourceParams>
</Context>

web工程里如下定义之后。就可进行使用
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4">
<description>MySQL Test App</description>
<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>
</web-app>
一个简单的jsp例子。

<%@ page contentType="text/html; charset=GBK"%>
<%@ page import="java.sql.*,javax.sql.DataSource,javax.naming.*"%>
<html>
<head><title>test.jsp</title></head>
<body bgcolor="#ffffff">
<h1>test Tomcat</h1>
<%
try
{
Context initCtx=new InitialContext();
DataSource ds = (DataSource)initCtx.lookup("java:comp/env/jdbc/TestDB");
Connection conn=ds.getConnection();
out.println("data from database:<br>");
Statement stmt=conn.createStatement();
ResultSet rs =stmt.executeQuery("select id, foo, bar from testdata");
while(rs.next())
{
out.println(rs.getInt("id"));
out.println(rs.getString("foo"));
out.println(rs.getString("bar"));
}
rs.close();
stmt.close();
}
catch(Exception e)
{
e.printStackTrace();
}
%>
</body>
</html>

上面在web中配置数据源,如果使用struts,可以在struts-config.xml里面配置。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: