我创建了一个jsp表单和一个servlet来使用tomcat在mysql中填充数据库,但是当我点击“提交”时。按钮将值传递给数据库,它显示表中0行已受影响的消息。在服务器日志中,它显示空指针异常。我已将MySQL JDBC驱动程序以及JSTL 1.2.2添加到库中。 我创建了两个排除的java类来建立数据库连接和执行查询。
以下是servlet代码:
public class SubmitUser extends HttpServlet {
private DbUpdate dbup = new DbUpdate();
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
String profession = request.getParameter("profession");
String features = request.getParameter("features");
String sql = "INSERT INTO test values('profession','features')";
int i = dbup.insertValues(sql);
这是包含查询执行的DbUpdate.java文件
public class DbUpdate {
private Statement st;
private int i;
private Connection c;
private DbConnection dbcon = new DbConnection();
public int insertValues(String sql){
try {
c = dbcon.connectDB();
Statement st = c.createStatement();
System.out.println("connected");
i = st.executeUpdate(sql);
} catch (SQLException ex) {
Logger.getLogger(DbConnection.class.getName()).log(Level.SEVERE, null, ex);
}
return i;
}
public static void main (String[] args){
//DbConnection dbcon =new DbConnection();
//dbcon.connectDB();
DbUpdate dbup = new DbUpdate();
}
}
最后建立连接的DbConnection.java文件
public class DbConnection {
private String driver = "com.mysql.jdbc.Driver";
private String url = "jdbc:mysql://localhost:3306/online_feedback";
private Connection con = null;
private Statement st;
//private int i;
public Connection connectDB(){
try {
Class.forName(driver);
Connection con = DriverManager.getConnection(url, "root", "");
System.out.println("Conn obj :::" + con);
} catch (ClassNotFoundException ex) {
Logger.getLogger(DbConnection.class.getName()).log(Level.SEVERE, null, ex);
} catch (SQLException ex) {
Logger.getLogger(DbConnection.class.getName()).log(Level.SEVERE, null, ex);
}
return con;
}
public static void main (String []args){
DbConnection db = new DbConnection();
db.connectDB();
}
}
我无法弄清楚出了什么问题
提前致谢
这是我在Servlet日志中获得的内容
SEVERE: Servlet.service() for servlet [SubmitUser] in context with path [/Feedback] threw exception
java.lang.NullPointerException
at feedback.DbUpdate.insertValues(DbUpdate.java:32)
at feedback.SubmitUser.processRequest(SubmitUser.java:76)
at feedback.SubmitUser.doGet(SubmitUser.java:120)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:936)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1004)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)

答案 0 :(得分:0)
我将假设你有一些重复的密钥检查。您的查询应该使用绑定参数,但您的SQL可以像
一样修复// String sql = "INSERT INTO test values('profession','features')";
String sql = "INSERT INTO test values('" + profession + "','"
+ features + "')";
目前您正在插入常量专业和功能。
使用PreparedStatement
,它可能看起来更像
public int insertValues(String profession, String features) {
String sql = "INSERT INTO test values(?, ?)";
Connection conn;
PreparedStatement ps;
try {
conn = dbcon.connectDB();
ps = conn.prepareStatement(sql);
ps.setString(1, profession);
ps.setString(2, features);
return ps.executeUpdate();
} catch (SQLException ex) {
Logger.getLogger(DbConnection.class.getName()).log(Level.SEVERE,
null, ex);
} finally {
if (ps != null) {
try {
ps.close();
} catch (Exception ignored) {
}
}
if (conn != null) {
try {
conn.close();
} catch (Exception ignored) {
}
}
}
return 0;
}