我使用PreparedStatement
运行以下代码。
<%
Class.forName("com.mysql.jdbc.Driver");
Connection Con=DriverManager.getConnection("jdbc:mysql://localhost:3306/onlineshopping","root","2710");
out.println("connection success full");%>
<%String username=request.getParameter("user_name");
String password=request.getParameter("password");
String cpassword=request.getParameter("cpassword");
String emailid=request.getParameter("email_id");
String address=request.getParameter("address");
String city=request.getParameter("city");
String pincode=request.getParameter("pincode");
String country=request.getParameter("country");
PreparedStatement pst = null;
if(password.equals(cpassword))
{
String str= "insert into register(user_name,password,email_id,address,city,pin,country) values(?,?,?,?,?,?,?)";
pst = Con.prepareStatement(str);
pst.setString(1,username);
pst.setString(2,password);
pst.setString(3,emailid);
pst.setString(4,address);
pst.setString(5,city);
pst.setString(6,pincode);
pst.setString(7,country);
int i = pst.executeUpdate();
String str1="insert into login(user_name,password) values (?,?)";
pst=Con.prepareStatement(str1);
pst.setString(1,username);
pst.setString(2,password);
int j=pst.executeUpdate();
if(i==0||j==0)
{
response.sendRedirect("registerError.jsp");
}
else
{
HttpSession ses=request.getSession(true);
session.setAttribute("user_name",username);
response.sendRedirect("loggedIn.jsp");
}
}
%>
但是,它会生成以下错误消息:
type Exception report
message An exception occurred processing JSP page /register.jsp at line 35
description The server encountered an internal error that prevented it from fulfilling this request.
exception
org.apache.jasper.JasperException: An exception occurred processing JSP page /register.jsp at line 35
32: pst.setString(5,city);
33: pst.setString(6,pincode);
34: pst.setString(7,country);
35: int i = pst.executeUpdate();
36: String str1="insert into login(user_name,password) values (?,?)";
37: pst=Con.prepareStatement(str1);
38: pst.setString(1,username);
Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:575)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:462)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
root cause
javax.servlet.ServletException: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Column 'user_name' cannot be null
org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:916)
org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:845)
org.apache.jsp.register_jsp._jspService(register_jsp.java:125)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:439)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
root cause
com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Column 'user_name' cannot be null
sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
java.lang.reflect.Constructor.newInstance(Constructor.java:408)
com.mysql.jdbc.Util.handleNewInstance(Util.java:409)
com.mysql.jdbc.Util.getInstance(Util.java:384)
com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1041)
com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3566)
com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3498)
com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1959)
com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2113)
com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2568)
com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2113)
com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2409)
com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2327)
com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2312)
org.apache.jsp.register_jsp._jspService(register_jsp.java:94)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:439)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
note The full stack trace of the root cause is available in the Apache Tomcat/7.0.63 logs.
我做错了什么?
答案 0 :(得分:1)
错误发生在你自己的帖子中..
例外情况:com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException
,邮件为Column 'user_name' cannot be null
。请检查user_name
字段是否为空。