Topic: 求教关于数据库连接池的问题

  Print this page

1.求教关于数据库连接池的问题 Copy to clipboard
Posted by: cocaocla
Posted on: 2005-09-06 16:03

我的调试环境是tomcat5。5,jdk1。5,mysql数据库。
之前用的是在javabean中写java代码的方法连接mysql数据库然后通过jsp网页访问,一切正常,今天改用了数据库连接池的方式连接数据库,通过一个简单的jsp测试网页显示连接成功了,然后我把连接池的代码照搬到javabean上就出现了问题,报错信息如下:
javax.servlet.ServletException: tried to access method bbs.dbConn.<init>()V from class org.apache.jsp.bbs.index_jsp
  org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:848)
  org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:781)
  org.apache.jsp.bbs.index_jsp._jspService(org.apache.jsp.bbs.index_jsp:98)
  org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
  javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
  org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:322)
  org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:291)
  org.apache.jasper.servlet.JspServlet.service(JspServlet.java:241)
  javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

root cause

java.lang.IllegalAccessError: tried to access method bbs.dbConn.<init>()V from class org.apache.jsp.bbs.index_jsp
  org.apache.jsp.bbs.index_jsp._jspService(org.apache.jsp.bbs.index_jsp:55)
  org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
  javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
  org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:322)
  org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:291)
  org.apache.jasper.servlet.JspServlet.service(JspServlet.java:241)
  javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

我的javabean代码如下:
package bbs;
import java.sql.*;
import javax.sql.*;
import javax.naming.*;
public class dbConn
{
  
  Connection conn=null;
  ResultSet rs=null;
  Statement stmt=null;
  
  public dbConn()
  {
    try
    {
      Context initCtx = new InitialContext();
Context ctx = (Context) initCtx.lookup("java:comp/env");
Object obj = (Object) ctx.lookup("JDBC for MySQL");
DataSource ds = (DataSource)obj;
conn=ds.getConnection();
    }
    catch(Exception e)
    {
      e.printStackTrace();
    }
    
  }
  
  public ResultSet executeQuery(String sql)
  {
    try
    {
      stmt=conn.createStatement();
     rs=stmt.executeQuery(sql);
    }
    catch(Exception e)
    {
      e.printStackTrace();
    }
    return rs;
  }
public int executeUpdate(String sql)
{
  int result=0;
   try
   {
     Statement stmt=conn.createStatement();
     result=stmt.executeUpdate(sql);
     
   }
   catch(Exception e)
   {
     e.printStackTrace();
   }
   return result;
  
}  

}

请问应如何解决? 最好用中文解答,我的英文实在太烂。在此先谢过各位

2.Re:求教关于数据库连接池的问题 [Re: cocaocla] Copy to clipboard
Posted by: benbon
Posted on: 2005-09-09 10:31

Context initCtx = new InitialContext();
Context ctx = (Context) initCtx.lookup("java:comp/env");
应该用你配置的数据源的信息去初始化你的Context;


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923