关闭

Tomcat6.0使用jdbc连接池连接mysql--操作步骤介绍

2220人阅读 评论(1) 收藏 举报

参考了部分网友文章,详尽地介绍操作步骤,使读者轻松入门。

1.       安装jdk,tomcat,mysql

jdk1.5.0_09:C:/Program Files/Java/jdk1.5.0_09

apache-tomcat-6.0.14.zip解压到C:/tomcat

mysql_noinstall_5_1_11_beta_win32.zip解压到C:/mysql

mysql-connector-java-5.1.3-rc.tar.gz解压到C:/Program Files/Java/jdk1.5.0_09/lib

 

2.       设置jdbc库classpath环境变量

Path= %JAVA_HOME%/bin;

classpath=.;C:/Program Files/Java/jdk1.5.0_09/lib/dt.jar;C:/Program Files/Java/j

dk1.5.0_09/lib/tools.jar;C:/tomcat/lib/servlet-api.jar;C:/Program Files/Java/jdk

1.5.0_09/lib/mysql-connector-java-5.1.3-rc-bin.jar;

CATALINA_HOME=c:/tomcat

JAVA_HOME=C:/Program Files/Java/jdk1.5.0_09

3.       部署web服务

 

PartI C:/tomcat/webapps/DBTest/index.jsp

<%@ page language="java" pageEncoding="UTF-8"%>

<%@ page import ="com.test.*" %>

<html>

<head>

<title>DB Test</title>

</head>

<body>

<%

DBTest tst = new DBTest();

tst.init();

%>

<h2>Results</h2>

Foo <%= tst.getFoo() %><br>

Bar <%= tst.getBar() %>

</body>

</html>

PartII C:/tomcat/webapps/DBTest/WEB-INF/classes/com/test/DBTest.class

编译,javac –d C:/tomcat/webapps/DBTest/WEB-INF/classes DBTest.java

/*
 * DBTest.java
 *
 * Created on 2007/06/07, 10:33:02
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

/**
 *
 * @author wangzicai
 */
import javax.naming.*;
import javax.sql.*;
import java.sql.*;
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/TestDB");
            if (ds != null) {
                Connection conn = ds.getConnection();
                if(conn != null) {
                    foo = "Got Connection "+conn.toString();
                    Statement stmt = conn.createStatement();
                    ResultSet rst =
                            stmt.executeQuery(
                            " select * from test ");
                    if(rst.next()) {
                        foo=rst.getString(1);
                        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)
    {
     try{
     DBTest db= new DBTest();
     db.init();
    }
    catch(Exception e)
    {e.printStackTrace();
    }
    }
}

 

PartIII C:/tomcat/webapps/DBTest/WEB-INF/web.xml

<?xml version="1.0" encoding="ISO-8859-1"?>

<web-app xmlns="http://java.sun.com/xml/ns/javaee"

   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

   xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"

   version="2.5">

    <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>

 

Part V C:/tomcat/webapps/DBTest/META-INF/Context.xml

<?xml version="1.0" encoding="UTF-8"?>

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

<Resource name="jdbc/TestDB" auth="Container" type="javax.sql.DataSource"

             maxActive="100" maxIdle="30" maxWait="10000"

             username="test1" password="abc" driverClassName="com.mysql.jdbc.Driver"

             url="jdbc:mysql://localhost:3306/bob?autoReconnect=true"/>

</Context>

 

4.       创建mysql数据库

mysql.exe –uroot

grant select,insert,update,delete on *.* to test1@"%" Identified by "abc";

create database bob;

use bob;

create table test  (id, int);

insert into table test set id=111;

exit;

 

5.       测试http://localhost:8080/DBTest/index.jsp

 

 
注:Tomcat6.0尚未发布admin模块,只能手工配置连接池。
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:9761次
    • 积分:126
    • 等级:
    • 排名:千里之外
    • 原创:4篇
    • 转载:0篇
    • 译文:0篇
    • 评论:1条
    最新评论