在jboss for mysql中出现连接池,错误500,NullPointer异常

时间:2016-03-09 12:13:03

标签: java mysql jdbc jboss jndi

我无法从数据源获取连接并返回错误-500 服务器遇到内部错误(),导致无法完成此请求。

public class Student extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
           String id                      = null;
           String pass                    = null;
           Authenticate a=new Authenticate();
         response.setContentType("text/html");
    PrintWriter out = response.getWriter();
        id = request.getParameter("name");
        pass = request.getParameter("password");
         String result=a.validate(id, pass);
         out.println(result);
         out.close();
     }
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request,response);

    }

}


public class Authenticate extends HttpServlet {

    DataSource datasource = null;
    String query = "SELECT COUNT(*)  FROM studentlogin WHERE name=? and password=?";

    public void init(ServletConfig config) throws ServletException {
        try {

            // Look up the JNDI data source only once at init time
            Context envCtx = new InitialContext();
            datasource = (DataSource) envCtx.lookup("java:/apalya");
            System.out.println("\t datasooource :: " + datasource);
            //datasource = (DataSource) ctx.lookup("apalya");
        } 
        catch (NamingException e) {
            e.printStackTrace();
        }
    }

    private Connection getConnection() throws SQLException {
        return datasource.getConnection();
    }

    public String validate(String name1, String password1) {
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultset = null;
        int count = 0;

        try {
            connection = getConnection();
            statement = connection.prepareStatement(query);
            statement.setString(1, name1);
            statement.setString(2, password1);
            resultset = statement.executeQuery();
            if (resultset.next()) {
                count = resultset.getInt(1);
            }

            if (count == 0) {
                return "Invalid Credentials";
            } else {
                return "Successfully Loggedin";
            }
        } catch (SQLException sqlException) {
            sqlException.printStackTrace();
            return "Internal Problem";
        } 

        finally {
            if (resultset != null) {
                try {
                    resultset.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }

            if (statement != null) {
                try {
                    statement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }

            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

我在getConnection方法中得到NullPointerException,并在浏览器中显示错误500。我在datasource xml文件中给出了相同的查找名称。

<servlet>
    <servlet-name>abc</servlet-name>
    <servlet-class>com.apalya.records.Student</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>abc</servlet-name>
    <url-pattern>/stu</url-pattern>
</servlet-mapping>
</web-app>

0 个答案:

没有答案