狂淘

www.kuangtao.net

   :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  6 随笔 :: 185 文章 :: 68 评论 :: 0 Trackbacks
最近在网上看到的java+jsp+ tomcat6+ mysql 连接池大多数是tomcat5 的,很多都说得不详细

1.需要的文件:mysql-5.0.27-win32.zip(安装文件),mysql-connector-java-5.0.4-bin.jar(连接驱动程序),apache-tomcat-6.0.10.exe(安装文件)

2.配置tomcat下的conf下的context.xml文件,在<context></context>之间添加连接池如下:

<Resource name="jdbc/mysql"  

       auth="Container"  

      type="javax.sql.DataSource"  

     driverClassName="com.mysql.jdbc.Driver"   
     url="jdbc:mysql://localhost/test"  

     username="root"  

     password="root"  

     maxActive="100"  

     maxIdle="30"  

     maxWait="10000" />  

3.配置你的应用下的web.xml中的<web-app></web-app>之间加入:  xml 代码

<resource-ref>  

    <description>DB Connection</description>  

    <res-ref-name>jdbc/mysqlx</res-ref-name>  

    <res-type>javax.sql.DataSource</res-type>  

    <res-auth>Container</res-auth>  

  </resource-ref>  

4.大功告成,不用在原来的server.xml里面配置了,下面就可以编写测试程序了,这个网上就很多了,主要的就上面,当然要把连接驱动程序都放到tomcat6下的lib下面.测试代码如下:


java 代码


  • <!doctype html public "-//w3c//dtd html 4.0 transitional//en"    
  •   
  • "http://www.w3.org/TR/REC-html40/strict.dtd">   
  •   
  • <%@ page import="java.sql.*"%>   
  •   
  • <%@ page import="javax.sql.*"%>   
  •   
  • <%@ page import="javax.naming.*"%>   
  •   
  • <%@ page session="false" %>   
  •   
  • <html>   
  •   
  • <head>   
  •   
  • <meta http-equiv="Content-Type" content="text/html; charset=gb2312">   
  •   
  • <title></title>   
  •   
  • <%    
  •   
  •    out.print("我的测试开始");   
  •   
  •    DataSource ds = null;   
  •   
  •    try{   
  •   
  •    InitialContext ctx=new InitialContext();   
  •   
  •    ds=(DataSource)ctx.lookup("java:comp/env/jdbc/mysql");   
  •   
  •    Connection conn = ds.getConnection();   
  •   
  •    Statement stmt = conn.createStatement();   
  •   
  •      //提示:users必须是数据库已有的表,   
  •   
  • //这里的数据库前文提及的Data Source URL配置里包含的数据库。   
  •   
  •    String strSql = " select * from users";   
  •   
  •    ResultSet rs = stmt.executeQuery(strSql);   
  •   
  •    while(rs.next()){   
  •   
  •       out.print(rs.getString(1));                    
  •   
  •      }   
  •   
  • out.print("我的测试结束");   
  •   
  •    }   
  •   
  •    catch(Exception ex){   
  •   
  •        out.print(“出现例外,信息是:”+ex.getMessage());   
  •   
  •     ex.printStackTrace();   
  •   
  •    }   
  •   
  • %>   
  •   
  • </head>   
  •   
  • <body>   
  •   
  • </body>   
  •   
  • </html>   
  • posted on 2009-10-09 10:13 狂淘 阅读(151) 评论(0)  编辑  收藏 所属分类: jsp项目 传到服务器问题

    只有注册用户登录后才能发表评论。


    网站导航: