服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

tomcat5.5+mysql5.0+eclipse连接池配置

一.tomcat5.5中没有admin模块,需要读者自行下载

二.tomcat中配置如下

jndi name: jdbc/mysql

data source url: jdbc:mysql://202.118.133.88/xscj

jdbc driver class: org.gjt.mm.mysql.driver

user name: root

password: ********

max. active connections: 4

max. idle connections: 2

max. wait for connection: 500

validation query:

注:

1.jdbc/mysql 前纲的jdbc也可以换成其它的, mysql为连池名,可任意起,在下文中注意使用2.jdbc:mysql://192.168.0.16/name(name为对应的数据库名也可以换成)

3.jdbc driver class : com.mysql.jdbc.driver(jdk中自带)

4.user name:为mysql中数据库管理员名

三. 下面是最关键的一点

d:/tomcat 5.5/confcontext.xml

<!-- the contents of this file will be loaded for each web application -->
<context>

    <!-- default set of monitored resources -->
    <watchedresource>web-inf/web.xml</watchedresource>
        
    <!-- uncomment this to disable session persistence across tomcat restarts -->
    <!--
    <manager pathname="" />
    -->
<resource
    name="mysql/xscj"
    type="javax.sql.datasource"
    password="kingsoft88"
    driverclassname="com.mysql.jdbc.driver"
    maxidle="2"
    maxwait="5000"
    username="root"
    url="jdbc:mysql://202.118.133.88:3306/xscj"
    maxactive="4"/>

</context>

相应的字段加对就可以了。

四.在eclipse中编译时加入tomcat 的dbcp和pool包就不会有问题了.

测试程序如下:

<html>
<head>
<title></title>
<% 
   out.print("开始测试:"+"<br/>");
   datasource ds = null;
   connection con=null;
   try{   
           context initctx = new initialcontext(); 
           context ctx = (context) initctx.lookup("java:comp/env"); 
                  //这里的数据库前文提及的data source url配置里包含的数据库。
           ds = (datasource)ctx.lookup("jdbc/xscj");
           con=ds.getconnection();
   statement stmt = con.createstatement();
   string strsql = "select * from xs";                       //表中的字段读者自行添加
   resultset rs = stmt.executequery(strsql);
   while(rs.next()){
      out.print(rs.getstring(1)+"<br/>");                 
     }
        rs.close();
        stmt.close();
       con.close();        
    out.print("我的测试结束");
   }
   catch(exception ex){
       out.print("出现例外,信息是:”+ ex.getmessage());
    ex.printstacktrace();
   }
%>
</head>
<body>
</body>
</html>

总结:以上步骤均十分关键,如果有误对应错误如下

1、第一步错误,报错

org.apache.commons.dbcp.sqlnestedexception: cannot create jdbc driver of class '' for connect url 'null'

2、第三步错误,报错

javax.naming.namenotfoundexception: name jdbc is not bound in this context

3、第四步错误,报错

java.sql.sqlexception: [microsoft][sqlserver 2000 driver for jdbc]error establishing socket

如果有上述错误,请检查对应步骤是否正确实施

扫描关注微信公众号