Исключение нулевого указателя при пуле соединений - PullRequest
1 голос
/ 16 февраля 2012

Я хочу попробовать простой пул соединений с базой данных на платформе Struts. Но не знаю, почему это произойдет через исключение нулевого указателя. Я опубликовал этот вопрос и хочу помочь вашему силду, чтобы это исправить. Заранее спасибо. Вот код, который я попробовал

index.jsp

<html>
    <body>
        <form action="some.do">
            <input type="submit">
        </form>
    </body>
</html>

TestDataSource.java

package test;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import java.sql.*;

public class TestDataSource extends Action
{
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws Exception
{

javax.sql.DataSource dataSource;
java.sql.Connection myConnection=null;
try
{
dataSource = getDataSource(request);
myConnection = dataSource.getConnection();
Statement stmt=myConnection.createStatement();
ResultSet rst=stmt.executeQuery("select name from studentinfo");
System.out.println("******************************************");
System.out.println("********Out Put from TestDataSource ******");
while(rst.next())
{
System.out.println("User Name is: " + rst.getString("name"));
}
System.out.println("******************************************");
rst.close();
stmt.close();
// do what you wish with myConnection
}
catch (SQLException sqle)
{
getServlet().log("Connection.process", sqle);
}
finally
{
//enclose this in a finally block to make
//sure the connection is closed
try
{
myConnection.close();
}
catch (SQLException e)
{
getServlet().log("Connection.close", e);
//e.printStackTrace();
}
}

return mapping.findForward("success");
}
}

распорка-config.xml

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE struts-config PUBLIC
          "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
          "http://jakarta.apache.org/struts/dtds/struts-config_1_3.dtd">


<struts-config>
    <data-sources>
<data-source type="org.apache.tomcat.dbcp.dbcp.BasicDataSource">
<set-property
property="driverClassName"
value="sun.jdbc.odbc.JdbcOdbcDriver" />
<set-property
property="url"
value="jdbc:odbc:mohit" />
<set-property
property="username"
value="system" />
<set-property
property="password"
value="rock" />
<set-property
property="maxActive"
value="10" />
<set-property
property="maxWait"
value="5000" />
<set-property
property="defaultAutoCommit"
value="false" />
<set-property
property="defaultReadOnly"
value="false" />
<set-property
property="validationQuery"
value="SELECT COUNT(*) FROM studentinfo" />
</data-source>
</data-sources> 
    <form-beans>

    </form-beans>
    <action-mappings>
        <action path="/some" type="test.TestDataSource">
            <forward name="success" path="/success.jsp"/>
        </action>
    </action-mappings>
</struts-config>

вот ошибка, которую я получил

SEVERE: Servlet.service() for servlet action threw exception
java.lang.NullPointerException
    at test.TestDataSource.execute(TestDataSource.java:51)
    at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:425)
    at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:228)
    at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1913)
    at org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:449)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:291)
    at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:877)
    at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:594)
    at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1675)
    at java.lang.Thread.run(Thread.java:619)
...