Tomcat 8 / MySql集成问题

时间:2016-06-09 22:57:51

标签: java mysql jsp tomcat netbeans

我遇到mySql与Tomcat 8集成的问题。我将详细说明我的步骤以及下面发生的错误。

  1. 我已将连接器jar添加到catalina_home / lib
  2. 我创建了一个名为testdb的全新数据库,并创建了一个名为TCmySqlSetup的新Web项目
  3. 在Meta-inf / context.xml中,我添加了:
  4.  
    <Context antiJARLocking="true" path="/TCmySqlSetup">    
        <Resource name="jdbc/testdb"
        auth="Container"    
        type="javax.sql.DataSource"    
        username="root"    
        password="myPass"    
        driverClassName="com.mysql.jdbc.Driver"    
        url="jdbc:mysql:/localhost:3306/testdb"    
        maxActive="15"    
        maxIdle="3"/>    
    </Context>
    
    1. 然后我创建了web.xml
    2.  
       <web-app>     
       <resource-ref>     
       <description>Db Conn</description>     
       <res-ref-name>jdbc/testdb</res-ref-name>     
       <res-type>javax.sql.DataSource</res-type>     
       <res-auth>Container</res-auth>     
       </resource-ref>     
       </web-app>
      
      1. 在我的index.JSP里面放了
      2.  // InitialContext ctx =null;
         DataSource ds =null;
         Statement stmt= null;
         Connection con = null;
         ResultSet rs = null;
         // try{ Class.forName("com.mysql.jdbc.Driver").newInstance();
         /* ctx = new InitialContext();
         ds = (DataSource) ctx.lookup("java:/comp/env/jdbc/testdb");*/
        
         Context initCtx = new InitialContext();
         Context envCtx = (Context) initCtx.lookup("java:comp/env");
         ds = (DataSource) envCtx.lookup("jdbc/testdb");
        
         con = ds.getConnection();
         stmt = con.createStatement();
        

        我没有复制整个代码,因为问题不在于此,它只是简单的列提取......

        然后当我尝试运行网站时,我得到了这个:

        org.apache.jasper.JasperException: An exception occurred processing
        JSP page /index.jsp at line 47
        
        44: 45: 46: 47: 
        con = ds.getConnection(); 48: stmt =
        con.createStatement(); 49: 50: rs =
        stmt.executeQuery("select name from employee");
        
        
        Stacktrace:
            org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:574)
            org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:461)
            org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396)
            org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340)
            javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
            org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
        root cause
        
        javax.servlet.ServletException: java.sql.SQLException: Cannot create
        JDBC driver of class 'com.mysql.jdbc.Driver' for connect URL
        'jdbc:mysql:/localhost:3306/MySQL56'
            org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:909)
            org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:838)
            org.apache.jsp.index_jsp._jspService(index_jsp.java:223)
            org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
            javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
            org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:438)
            org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396)
            org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340)
            javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
            org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
        root cause
        
        java.sql.SQLException: Cannot create JDBC driver of class
        'com.mysql.jdbc.Driver' for connect URL
        'jdbc:mysql:/localhost:3306/MySQL56'
        org.apache.tomcat.dbcp.dbcp2.BasicDataSource.createConnectionFactory(BasicDataSource.java:2160)
            org.apache.tomcat.dbcp.dbcp2.BasicDataSource.createDataSource(BasicDataSource.java:2032)
            org.apache.tomcat.dbcp.dbcp2.BasicDataSource.getConnection(BasicDataSource.java:1532)
            org.apache.jsp.index_jsp._jspService(index_jsp.java:164)
            org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
            javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
            org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:438)
            org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396)
            org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340)
            javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
            org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
        root cause
        
        java.sql.SQLException: No suitable driver
        org.apache.tomcat.dbcp.dbcp2.BasicDataSource.createConnectionFactory(BasicDataSource.java:2151)
            org.apache.tomcat.dbcp.dbcp2.BasicDataSource.createDataSource(BasicDataSource.java:2032)
            org.apache.tomcat.dbcp.dbcp2.BasicDataSource.getConnection(BasicDataSource.java:1532)
            org.apache.jsp.index_jsp._jspService(index_jsp.java:164)
            org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
            javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
            org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:438)
            org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396)
            org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340)
            javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
            org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
        

        我一直在和它斗争很长一段时间,我真的想要做得对,我的大部分代码都适用于我的真实环境,似乎我可以连接而不会以某种方式汇集这段代码

        Class.forName("com.mysql.jdbc.Driver").newInstance();
        
         java.sql.Connection conn;
        
         conn = DriverManager.getConnection("jdbc:mysql://localhost/testdb","root","peskir123");
         Statement sqlStatement = conn.createStatement();
        

0 个答案:

没有答案