现在的位置: 首页 > 综合 > 正文

tomcat 5.5 mysql 连接池

2013年10月17日 ⁄ 综合 ⁄ 共 1770字 ⁄ 字号 评论关闭

jsp 倒很容易配出来。conf/context.xml

<Resource
         name="jdbc/person"
         type="javax.sql.DataSource"
         password="root"
         driverClassName="com.mysql.jdbc.Driver"
         maxIdle="2"
         maxWait="5000"
         username="root"
         url="jdbc:mysql://localhost:3306/person"
         maxActive="4"/>    

然后直接用就可以了。

但封装到java 里我到现在没搞出来,不知道是什么原因。

错误提示。

 

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

java.lang.ClassCastException: org.apache.tomcat.dbcp.dbcp.BasicDataSource cannot be cast to sun.jdbc.odbc.ee.DataSource
	rubicon.ConnDB.getConn(ConnDB.java:23)
	rubicon.BeanCl.getResultByPage(BeanCl.java:23)
	rubicon.Wel.doPost(Wel.java:42)
	rubicon.Wel.doGet(Wel.java:18)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:627)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:729)

 

note The full stack trace of the root cause is available in the Apache Tomcat/5.5.28 logs.


 

 

public Connection getConn(){

 
   try {
    Context ctx=new InitialContext();
       DataSource ds=(DataSource)ctx.lookup("java:comp/env/jdbc/person");  
       conn = ds.getConnection();
   } catch (NamingException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }   
  
  return conn;
    
    
 /* String url = "jdbc:mysql://localhost:3306/person";
  
  try {
   Class.forName("com.mysql.jdbc.Driver");
   conn=DriverManager.getConnection(url,"root","root");
  } catch (ClassNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
   System.out.println("无法加载驱动程序");
  }catch (SQLException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
   System.out.println("无法连接驱动程序");
  }
  return conn;*/

 }

我用绿色文字(注释)的就成功,用上面这段就不成功。苦恼啊。这个问题我先放着,留着以后解决。路过的朋友也帮忙看看。

抱歉!评论已关闭.