2

我的问题与 [Link] 非常相似:(Why Servlet.service() for servlet [XYZ] in context with path [/ABC] throw exception)但我认为它没有得到解决。

这是一个简单的 SQL 网关应用程序,在我的个人计算机 localhost 上运行良好。但是,当我将战争文件上传到服务器时,它会引发此异常

org.apache.catalina.core.StandardWrapperValve invoke SEVERE: Servlet.service() for servlet [SqlGatewayServlet] in context with path [/DEV-JDBC] threw exception java.lang.NullPointerException at sql.SqlGatewayServlet.do

java.lang.NullPointerException sql.SqlGatewayServlet.doPost(SqlGatewayServlet.java:34)

线: 34 是Statement statement = connection.createStatement();

SqlGatewayServlet

public class SqlGatewayServlet extends HttpServlet {

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    String sqlStatement = request.getParameter("sqlStatement");
    String sqlResult = "";



    try{
        ConnectionPool pool = ConnectionPool.getInstance();
        Connection connection = pool.getConnection();

        // create a statement
        Statement statement = connection.createStatement();

        // parse the SQL string
        sqlStatement = sqlStatement.trim();
        if(sqlStatement.length() > 6){

            String sqlType = sqlStatement.substring(0, 6);
            if(sqlType.equalsIgnoreCase("select")){

                // create the HTML for the result set
                ResultSet resultSet = statement.executeQuery(sqlStatement);
                sqlResult = SQLUtil.getHtmlTable(resultSet);
                resultSet.close();

            }else{

                int i = statement.executeUpdate(sqlStatement);
                // a DDL statement
                if(i == 0){
                    sqlResult = "The statement executed successfully";
                }else{
                    sqlResult = "The statment executed sucessfully.<br>"
                            + i + "row(s) affected.";
                }                   
            }
        statement.close();
        connection.close();

        }

    }catch( SQLException e){
        sqlResult = "Error executing the SQL statement: <br>"
                + e.getMessage();

    }
    HttpSession session = request.getSession();
    session.setAttribute("sqlResult", sqlResult);
    session.setAttribute("sqlStatement", sqlStatement);

    String url = "/index.jsp";

    RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(url);

    dispatcher.forward(request, response);
}

}

上下文.xml

<Context path="/DEV-JDBC">

<Resource name="jdbc/uplink_project" auth="Container" 
    maxActive="100" maxIdle="30" maxWait="10000" 
    username="xxxxx" password="xxxxxx" 
    driverClassName="com.mysql.jdbc.Driver" 
    url="jdbc:mysql://localhost:2082/uplink_project?autoReconnect=true" 
    logAbandoned="true" removeAbandoned="true" 
    removeAbandonedTimeout="60" type="javax.sql.DataSource" />

我的设置有问题吗?还是它的服务器问题?请让我知道您还需要什么其他信息。

预先感谢

4

2 回答 2

2

ConnectionPool可能问题出在方法上的类上,getConnection()因为connectionnull.

于 2013-08-23T03:08:10.113 回答
1

如果以下语句出现空指针异常:

   Statement statement = connection.createStatement();

那么这意味着您的连接对象为空。

当您从 ConnectionPool 类初始化连接对象时。所以确保它的 getConnection 方法返回一个有效的连接对象:

    ConnectionPool pool = ConnectionPool.getInstance();
    Connection connection = pool.getConnection();

您需要调试 ConnectionPool 类。

于 2013-08-23T03:13:03.527 回答