tomcat配置dbcp数据库连接池及其碰到的错误

在jsp中配置数据库连接池的步骤.

1.要在tomcat下的conf包中的server.xml中加入数据库连接池配置信息

 

  1. <Context path="/jdbc" docBase="D:/workspace/jdbc/WebRoot" > 
  2. <Resource auth="Container" name="jdbc/hhh" type="javax.sql.DataSource" factory="org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory" 
  3.  driverClassName="com.mysql.jdbc.Driver"      url="jdbc:mysql://localhost:3306/hanyue" 
  4. username="root" 
  5. password="" 
  6. maxActive="10000" 
  7. maxIdle="10000" 
  8. maxWait="10000" 
  9. removeAbandoned="true" 
  10. removeAbandonedTimeOut="10" 
  11. logAbandoned="true"/>
  12.               
  13. </Context>

1.确认common/lib/naming-factory-dbcp.jar存在.添加factory的时候一定要写org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory,

而不能写org.apache.commons.dbcp.BasicDataSourceFactory,否则会报找不到驱动程序的url的错误.

2.注意:

path:Web应用程序的上下文路径。(就是你的项目 名前加上以个 “/”)
docBase:表示的项目的具体路径,如果你的项目是在D:/demo,这里就应该是D:/Demo
该测试项目路径 D:/workspace/jdbc/WebRoot
如果测试项目的文件位置,是在tomcat/webapps/Demo中,所以属性为Demo
< Resource >元素为JNDI,在lookup是要查找的资源
name: 表示JNDI在lookup是输入的 资源名
3.
  1. <%@ page import="java.sql.*" %>
  2. <%@ page import="javax.sql.*" %>
  3. <%@ page import="javax.naming.*" %>
  4. <%@ page contentType="text/html;charset=GB2312" %>
  5. <html>
  6. <head>
  7.   <title>my jdbc action</title>
  8. </head>
  9. <body>
  10. <h2>登陆结果</h2>
  11. <%
  12.     Connection con = null;  
  13.     Statement stmt = null;  
  14.     Statement stmt1 = null
  15.     ResultSet rs = null;
  16.     String url = "jdbc:mysql://localhost:3306/hanyue";
  17.     String uname = "root";
  18.     String upasswd = "";
  19.     DataSource ds = null;
  20.     
  21.     String id = "13";   
  22.     String name = request.getParameter("name"); 
  23.     String passwd = request.getParameter("passwd");     
  24.     String new_last_name = "";
  25.     String new_first_name = ""
  26. %>
  27.         从login.html得到值
  28.         name = <%= name %>  
  29.         passwd = <%= passwd %><br><br>
  30.     
  31. <%      
  32.     try 
  33.     {   
  34.          //找到驱动程序并注册
  35.         Context initContext = new InitialContext();
  36.         Context envContext  = (Context)initContext.lookup("java:/comp/env");
  37.         DataSource dst = (DataSource)envContext.lookup("jdbc/hhh");
  38.          //ds = (DataSource) initContext.lookup("java:comp/env/jdbc/hhh");
  39.         con = dst.getConnection();
  40.         if(envContext==null){
  41.             System.out.println("envContext is null");
  42.         }else{
  43.             System.out.println("envContext is not null");
  44.         }
  45.         
  46.         if(dst==null){
  47.             System.out.println("dst is null");
  48.         }else{
  49.             System.out.println("dst is not null");
  50.         }
  51.         
  52.         if(con==null){
  53.             System.out.println("con is null");
  54.         }else{
  55.             System.out.println("con is not null");
  56.         }
  57.         //stmt = con.createStatement();
  58.         
  59.     //  Class.forName("org.gjt.mm.mysql.Driver");
  60.     //  con = DriverManager.getConnection(url,uname,upasswd);
  61.      //   stmt = con.createStatement();
  62.         //String upd = "insert into person values('"+id+"','"+name+"','"+passwd+"')";
  63.         //stmt.executeUpdate(upd);     
  64.         stmt1 = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);   
  65.         String query = "SELECT * FROM person";  
  66.         rs = stmt1.executeQuery(query); 
  67.     
  68.         rs.last( );         
  69.         new_last_name = rs.getString("name");   
  70.         new_first_name = rs.getString("password");
  71.     
  72.     }
  73.     catch(SQLException e){
  74.         e.printStackTrace();
  75.     }
  76.     
  77.     finally
  78.     {
  79.         try {
  80.             if(con != null)
  81.             {
  82.                 con.close();
  83.             }       
  84.         }
  85.         catch(SQLException sqle)
  86.         {
  87.             out.println("sqle="+sqle);  
  88.         }
  89.     }
  90. %>
  91.    从数据库中取得的数据:
  92.   <% out.println("name = "+new_last_name);
  93.    out.println("passwd = "+new_first_name);%>
  94. </body>
  95. </html>

测试界面:

<html>
<head>
  <title>my jdbc</title>
<meta http-equiv="Content-Type" content="text/html; charset=GB2312">
</head>
<body>
 
<h2>登陆信息</h2>
<form name="form" action="action.jsp" method="post"  >
  <p><input name="name" type="text"></p>
  <p><input name="passwd" type="password"></p>
  <p>
    <input type="submit" value="确定">
    <input type="reset" value="清空">
  </p>
</form>

</body>
</html>

 

数据库用mysql...测试结果...打印出了刚存入的数据.数据库连接池测试成功!!!!

问题:为什么用org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory的不行.

        为什么该工程下的web.xml的东西注册不注册都行???

<resource-ref>
    <description>DB Connection</description>
    <res-ref-name>jdbc/hanyue</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
  </resource-ref>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值