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

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

1.       安装jdk,tomcat,mysql

jdk 1.5.0 _09:C:/Program Files/Java/jdk 1.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/jdk 1.5.0 _09/lib/dt.jar;C:/Program Files/Java/j

dk 1.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/jdk 1.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模块,只能手工配置连接池。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值