抛出NullPointerException

时间:2017-05-16 14:14:22

标签: java tomcat jdbc datasource jndi

我正在学习为我的Web应用程序设置数据库连接,并且遇到了this链接。我使用TomCat跟踪它的示例,并使用我的数据库信息配置conf / Server.xml。这是我的Conf / server.xml文件 -

<Resource name="jdbc/MyMagnumFt" 
      global="jdbc/MyMagnumFt" 
      auth="Container" 
      type="javax.sql.DataSource" 
      driverClassName="oracle.jdbc.driver.OracleDriver" 
      url="jdbc:oracle:thin:@<<myCorrectURL>>" 
      username="user" 
      password="pass"     
      maxActive="100" 
      maxIdle="20" 
      minIdle="5" 
      maxWait="10000"/>

上面链接中的示例要求在context.xml文件中创建ResourceLink。所以我在conf / context.xml文件中创建了一个ResourceLink -

<ResourceLink name="jdbc/MyLocalMagnumFt"
            global="jdbc/MyMagnumFt"
            auth="Container"
            type="javax.sql.DataSource" />

现在,此示例未指定有关在web.xml文件中进行更改的任何内容。所以我没有为此烦恼。

这是我试图获得连接的servlet代码 -

public class ResourcePool extends HttpServlet {
private static final long serialVersionUID = 1L;

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    InitialContext ctx = null;
    Connection con = null;
    Statement stmt = null;
    ResultSet rs = null;
    try{
        ctx = new InitialContext();
        DataSource ds = (DataSource) ctx.lookup("java:/comp/env/jdbc/MyLocalMagnumFt");

        con = ds.getConnection();
        stmt = con.createStatement();

rs = stmt.executeQuery("select empid, name from Employee");

lookup()工作文件并获取我在server.xml中编码的数据源信息。但是,当控制转到getConnection()语句时,它退出try块并进入finally块的rs.close()语句。在控制台中我看到以下错误 -

May 15, 2017 4:27:46 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [ResourcePool] in context with path [/myJNDI2] threw exception
java.lang.NullPointerException
    at org.ashu.tester.ResourcePool.doGet(ResourcePool.java:65)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:230)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:474)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:624)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:349)
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:783)
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:798)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1434)
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:745)

现在我探讨了不同的帖子,将数据源配置为在Application的context.xml中设置数据库信息并在web.xml中创建一个条目,但我想试试这个例子并设置一个全局数据源应用程序可以使用。

如果我在这里遗漏了某些内容或者您需要更多详细信息,请告知我们。这是我的项目设置的屏幕截图 - Project Setup

这是完整的代码 -

package org.ashu.tester;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;

public class ResourcePool extends HttpServlet {
private static final long serialVersionUID = 1L;

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    InitialContext ctx = null;
    Connection con = null;
    Statement stmt = null;
    ResultSet rs = null;
    try{
        ctx = new InitialContext();
        DataSource ds = (DataSource) ctx.lookup("java:/comp/env/jdbc/MyLocalMagnumFt");

        con = ds.getConnection();
        stmt = con.createStatement();

        rs = stmt.executeQuery("select empid, name from Employee");

        PrintWriter out = response.getWriter();
        response.setContentType("text/html");
        out.print("<html><body><h2>Employee Details</h2>");
        out.print("<table border=\"1\" cellspacing=10 cellpadding=5>");
        out.print("<th>Employee ID</th>");
        out.print("<th>Employee Name</th>");

        while(rs.next())
        {
            out.print("<tr>");
            out.print("<td>" + rs.getInt("empid") + "</td>");
            out.print("<td>" + rs.getString("name") + "</td>");
            out.print("</tr>");
        }
        out.print("</table></body><br/>");

        //lets print some DB information
        out.print("<h3>Database Details</h3>");
        out.print("Database Product: "+con.getMetaData().getDatabaseProductName()+"<br/>");
        out.print("Database Driver: "+con.getMetaData().getDriverName());
        out.print("</html>");

    }catch(NamingException e){
        e.printStackTrace();
    } catch (SQLException e) {
        e.printStackTrace();
    }finally{
        try {
            rs.close();
            stmt.close();
            con.close();
            ctx.close();
        } catch (SQLException e) {
            System.out.println("Exception in closing DB resources");
        } catch (NamingException e) {
            System.out.println("Exception in closing Context");
        }

    }
}

}

0 个答案:

没有答案