我有一个表单,用于向Servlet提交帖子请求。我创建了一个类DbConnection
来管理对MySQL数据库的身份验证,当我使用静态main方法测试它时,它非常有效。当我在Servlet中使用这个类时,它一直给我这个错误。
我的代码出了什么问题?
这是DbConnection类。
package com.cloud.db;
public class DbConnection {
private Connection conn;
private String user = NAME;
private String passwd = SOME_PASSWORD;
private Statement stmt;
private ResultSet result;
private PreparedStatement auth;
public DbConnection() {
try {
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/cloudbase", user, passwd);
stmt = conn.createStatement();
} catch (SQLException e) {
e.printStackTrace();
}
}
public boolean authenticate(String usrname, String pwd) throws SQLException {
PreparedStatement stm = conn.prepareStatement("select * from users where email=? and password = ?");
stm.setString(1, usrname);
stm.setString(2, pwd);
ResultSet result = stm.executeQuery();
if (result.next()) {
return true;
} else {
return false;
}
}
}
这是我的servlet
package com.cloud.db;
import com.cloud.*;
import java.sql.*;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/auth")
public class Auth extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public Auth() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String email = request.getParameter("email");
String passwd = request.getParameter("pwd");
DbConnection connect = new DbConnection();
try {
new DbConnection().authenticate("example@cloudserver.com", "password");
} catch (SQLException e) {
System.out.println("Error :" + e.getMessage());
}
}
}
答案 0 :(得分:1)
根据您发布的内容,我说问题就在这里:
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/cloudbase",user,passwd);
stmt = conn.createStatement();
DriverManager.getConnection
正在返回null
,而null.create ......正在抛出NPE。
如何验证:在conn = ...
之后放置一个sysout,看看连接是否为空
如何解决这个问题:
1)确保驱动程序类在类路径中并且已加载。
2)在使用对象之前,请确保它已初始化。
答案 1 :(得分:1)
在本地运行代码和在tomcat中运行代码的最大区别是JDBC驱动程序的位置,因此最可能的问题是Tomcat无法找到JDBC驱动程序。 Tomcat希望在$ CATALINA_HOME / lib中找到一个驱动程序。有关于设置MySQL数据源here的说明。
目前,您的异常处理模糊了正在发生的事情。原始问题发生在DBConnection构造函数中,但由于异常被捕获,authenticate方法继续运行并使用null连接运行,从而导致NullPointerException。
Tomcat应该将DBConnection构造函数中生成的异常写入控制台,因此您可以在那里查找原始问题。要修复异常处理,可以将DbConnection代码更改为:
public boolean authenticate(String usrname,String pwd) throws SQLException {
Connection conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/cloudbase",user,passwd);
try {
PreparedStatement stm = conn.prepareStatement(
"select * from users where email=? and password = ?");
stm.setString(1, usrname);
stm.setString(2, pwd);
ResultSet result = stm.executeQuery();
return result.next();
} finally {
try {
conn.close();
} catch (SQLException e) {
// use a logger instead of stdout
log.info("close caused exception", e);
}
}
}
这可以确保抛出的异常是导致原始问题的异常。
答案 2 :(得分:1)
你也可以这样做
public class ConnectionProvider {
public static Connection getConnection() throws SQLException {
Connection connection = null;
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/cloudbase"
,user,passwd);
return connection;
}
public void authenticate(Connection connection,String usrname,String pwd)
throws SQLException {
String query = "select firstName from users where email=? and password = ?";
try {
PreparedStatement stm = connection.prepareStatement(query);
stm.setString(1, usrname);
stm.setString(2, pwd);
ResultSet result = stm.executeQuery();
if (resultSet.next()) {
do {
System.out.println("Welcome, "+resultSet.getString("firstName"));
} while(resultSet.next());
}
else{
System.out.println("Username or id doesnot exist");
}
} catch (SQLException e) {
System.out.println("Error :" + e.getMessage());
}
finally{
connection.close();
}
}
然后在你的Servlet中
protected void doPost(...) throws ServletException, IOException {
String email = request.getParameter("email");
String passwd = request.getParameter("pwd");
Connection connection = ConnectionProvider.getConnection();
new ConnectionProvider().authenticate(connection,email ,passwd);
}
答案 3 :(得分:1)
你没有加载mysql驱动程序,这就是为什么drivermanager返回null。在JDBC中,我们必须注册Driver(在你的情况下是Mysql驱动程序)。
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/cloudbase", user, passwd);
/* your remaining code ....
...*/