0

我正在尝试创建我的第一个连接池。我正在使用 Tomcat 7 和 MySQL DB 创建一个 Java Web 应用程序,并且我想创建一个可能实现连接池的 Servelet。我尝试了一个网络代码,但出现了一个持久的 HTTP 500 问题。我搜索了很多,但仍然没有解决方案,所以请不要链接其他讨论,因为我已经全部检查过了!这是我的代码:对于 context.xml :

    <?xml version="1.0" encoding="UTF-8"?>
    <Context antiJARLocking="true" path="/My_First_WEB"
    reloadable="true" 
docBase="\My_First_WEB" >

<Resource
    name="jdbc/TutoPool"
    auth="Container"
    type="javax.sql.DataSource"/>

<ResourceParams name="jdbc/TutoPool">
    <parameter>
        <name>username</name>
        <value>root</value>
    </parameter>
    <parameter>
        <name>password</name>
        <value>""</value>
    </parameter>
    <parameter>
        <name>driverClassName</name>
        <value>com.mysql.jdbc.Driver</value>
    </parameter>
    <parameter>
        <name>url</name>
        <value>jdbc:mysql://localhost/TutoPool</value>
    </parameter>
                <parameter>
    <name>maxActive</name>
    <value>8</value>
</parameter>
<parameter>
    <name>maxIdle</name>
    <value>8</value>
</parameter>
<parameter>
    <name>maxWait</name>
    <value>10000</value>
</parameter>
<parameter>
    <name>validationQuery</name>
    <value>select 1</value>
</parameter>
<parameter>
    <name>removeAbandoned</name>
    <value>true</value>
</parameter>
<parameter>
    <name>removeAbandonedTimeout</name>
    <value>20</value>
</parameter>
<parameter>
    <name>logAbandoned</name>
    <value>true</value>
</parameter>
</ResourceParams>        
</Context>

web.xml 如下:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
    <servlet>
    <servlet-name>TutoPool</servlet-name>
    <servlet-class>MyServelets.TutoPool</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>TutoPool</servlet-name>
    <url-pattern>/TutoPool</url-pattern>
</servlet-mapping>
<resource-ref>
    <description>
        reference a la ressource BDD pour le pool
    </description>
    <res-ref-name>jdbc/TutoPool</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
</resource-ref> 
<session-config>
    <session-timeout>
        30
    </session-timeout>
</session-config>

最后 TutoPool.java 如下:

 package MyServelets;

 import java.io.*;
 import java.sql.*;
 import javax.naming.*;
 import javax.servlet.ServletException;
 import javax.servlet.UnavailableException;
 import javax.servlet.annotation.WebServlet;
 import javax.servlet.http.*;
 import javax.sql.DataSource;


 @WebServlet(name = "TutoPool", urlPatterns = {"/TutoPool"})
 public class TutoPool extends HttpServlet {
    private DataSource ds; //la source de données



/**
 * Processes requests for both HTTP
 * <code>GET</code> and
 * <code>POST</code> methods.
 *
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();
    try {
        /* TODO output your page here. You may use following sample code. */
        out.println("<html>");
        out.println("<head>");
        out.println("<title>Servlet TutoPool</title>");            
        out.println("</head>");
        out.println("<body>");
        out.println("<h1>Servlet TutoPool at " + request.getContextPath() + "</h1>");
        out.println("</body>");
        out.println("</html>");
    } finally {            
        out.close();
    }
}

// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
 * Handles the HTTP
 * <code>GET</code> method.
 *
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    response.setContentType("text/html");
    PrintWriter out = response.getWriter();

    out.println("<html><head></head>");
    out.println("<body>");

    Connection con=null;
    Statement s=null;
    ResultSet rs=null;

    try {
        //récupération de la Connection depuis le DataSource
        con = ds.getConnection();
        s = con.createStatement();
        rs = s.executeQuery("SELECT * FROM table1");
        while (rs.next()) {
            out.println(rs.getString(1) + "      ");
            out.println(rs.getString(2) + "<br/>");
        }
    } catch (SQLException e) {
        response.sendError(500, "Exception sur l'accès à la BDD " + e);
    }finally {
        if (rs != null)
        {
            try {
                rs.close();
            } catch (SQLException e) {}
            rs = null;
        }
        if (s != null) {
            try {
                s.close();
            } catch (SQLException e) {}
            s = null;
        }
        if (con != null) {
            try {
                con.close();
            } catch (SQLException e) {}
            con = null;
        }
    }
    out.println("</body>");
    out.println("</html>");
    out.close();
}

/**
 * Returns a short description of the servlet.
 *
 * @return a String containing servlet description
 */
@Override
public String getServletInfo() {
    return "Short description";
}// </editor-fold>

/**
 *
 * @throws ServletException
 */
@Override
public void init() throws ServletException {
    try {
        //récupération de la source de donnée
        Context initCtx = new InitialContext();
        ds = (DataSource) initCtx.lookup("java:comp/env/jdbc/TutoPool");
    } catch (Exception e) {
        throw new UnavailableException(e.getMessage());
    }
}

}

最后出现的错误是:

type Rapport d''état

message Exception sur l'accès à la BDD org.apache.tomcat.dbcp.dbcp.SQLNestedException:     Cannot create JDBC driver of class '' for connect URL 'null'

description Le serveur a rencontré une erreur interne (Exception sur l'accès à la BDD  org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create JDBC driver of class '' for  connect URL 'null') qui l'a empêché de satisfaire la requête.

我已经检查了图书馆中的 mysql-connector,这应该可以工作!还是没有。请问,有人对此有想法吗?我在这里淹死了!!:( 谢谢。

4

1 回答 1

1

根据消息,连接池没有找到您的驱动程序类的名称,也没有找到您的数据库的 URL。所以我认为这是一个配置问题。

根据Tomcat 7 文档,您应该以这种方式配置连接池:

  <Resource name="jdbc/TestDB" auth="Container" type="javax.sql.DataSource"
           maxActive="100" maxIdle="30" maxWait="10000"
           username="root" password="" driverClassName="com.mysql.jdbc.Driver"
           url="jdbc:mysql://localhost/TutoPool"/>

你也试过吗?

于 2013-04-09T10:09:43.087 回答