tomcat连mysql数据源的配置

tomcat-5.5.17连mysql5.1.11数据源的配置

以前一直用jboss连mysql,数据源配置一直很顺利,现在需要用tomcat了,可是配数据源总是出不来,郁闷了半天,借鉴了网上朋友的经验终于出来了,现在这里总结一下配置过程:

项目名称为DBTest。

一、给项目配置一个数据源。方法一

1.D:/tomcat-5.5.17/conf/Catalina/localhost/目录下新建文件DBTest.xml,内容如下:

<Context path="/DBTest" docBase="DBTest"
        debug="5" reloadable="true" crossContext="true">

  <Resource name="jdbc/mysqlTest" auth="Container" type="javax.sql.DataSource"
               maxActive="100" maxIdle="30" maxWait="10000"
               username="root" password="xxxxxx" driverClassName="com.mysql.jdbc.Driver"
               url="jdbc:mysql://localhost:3306/test?autoReconnect=true"/>

</Context>

 

2.dbtest/WEB-INF/web.xml内容如下:

<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/mysqlTest</res-ref-name>
      <res-type>javax.sql.DataSource</res-type>
      <res-auth>Container</res-auth>
  </resource-ref>
</web-app>

3.测试用的index.jsp,内容如下

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>DataSourse Testing..</title>
</head>
<body>
<%
javax.naming.Context ctx = new javax.naming.InitialContext();
javax.sql.DataSource ds = (javax.sql.DataSource)ctx.lookup("java:/comp/env/jdbc/mysqlTest");
out.println("DataSource = "+ds.toString());
java.sql.Connection conn = ds.getConnection();
out.println("<br/>conn = "+conn);
%>
</body>
</html>

启动tomcat,在ie中输入http://localhost:8080/DBTest, 显示出datasource和conn对象,说明配置成功。

方法二

把DBTest.xml文件改名为context.xml,放到项目DBTest/META-INF/目录下。其他配置和方法一完全一致。

方法三

把DBTest.xml文件的内容放到D:/tomcat-5.5.17/conf/server.xml中去,放在</Host>的前面。其他配置和方法一完全一致。

二、给项目配置2个数据源。这里只介绍与上面方法二种对应的方法,其他两种方法类似

1.context.xml放到项目DBTest/META-INF/目录下,内容如下(注意根据自身情况修改下面的用户名和密码):

<Context path="/DBTest" docBase="DBTest"
        debug="5" reloadable="true" crossContext="true">

  <Resource name="jdbc/mysqlTest" auth="Container" type="javax.sql.DataSource"
               maxActive="100" maxIdle="30" maxWait="10000"
               username="root" password="xxxxxx" driverClassName="com.mysql.jdbc.Driver"
               url="jdbc:mysql://localhost:3306/test?autoReconnect=true"/>

  <Resource name="jdbc/mysqlTest2" auth="Container" type="javax.sql.DataSource"
               maxActive="100" maxIdle="30" maxWait="10000"
               username="javauser" password="javadude" driverClassName="com.mysql.jdbc.Driver"
               url="jdbc:mysql://localhost:3306/javatest?autoReconnect=true"/>
</Context>

2.web.xml放到项目DBTest/WEB-INF/目录下,内容如下:

<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/mysqlTest</res-ref-name>
      <res-type>javax.sql.DataSource</res-type>
      <res-auth>Container</res-auth>
  </resource-ref>
 
  <description>MySQL Test App2</description>
  <resource-ref>
      <description>DB Connection</description>
      <res-ref-name>jdbc/mysqlTest2</res-ref-name>
      <res-type>javax.sql.DataSource</res-type>
      <res-auth>Container</res-auth>
  </resource-ref>
</web-app>

3.测试用的index.jsp,内容如下:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>DataSourse Testing..</title>
</head>
<body>
<%
javax.naming.Context ctx = new javax.naming.InitialContext();
javax.sql.DataSource ds = (javax.sql.DataSource)ctx.lookup("java:/comp/env/jdbc/mysqlTest");
out.println("<br/>DataSource = "+ds.toString());
java.sql.Connection conn = ds.getConnection();
out.println("<br/>conn = "+conn);

javax.naming.Context ctx2 = new javax.naming.InitialContext();
javax.sql.DataSource ds2 = (javax.sql.DataSource)ctx2.lookup("java:/comp/env/jdbc/mysqlTest2");
out.println("<br/><br/>DataSource2 = "+ds2.toString());
java.sql.Connection conn2 = ds2.getConnection();
out.println("<br/>conn2 = "+conn2);
%>
</body>
</html>

启动MySQL和tomcat后,运行结果显示如下

DataSource = org.apache.tomcat.dbcp.dbcp.BasicDataSource@e753
conn = org.apache.tomcat.dbcp.dbcp.PoolableConnection@18c3679

DataSource2 = org.apache.tomcat.dbcp.dbcp.BasicDataSource@4c47db
conn2 = org.apache.tomcat.dbcp.dbcp.PoolableConnection@16be68f

阅读更多

没有更多推荐了,返回首页