我的JSP在将数据插入mysql数据库时遇到错误

时间:2016-09-24 13:59:27

标签: mysql jsp

我收到以下错误:

  

com.mysql.jdbc.exceptions.MySQLSyntaxErrorException:未知列   '两性'在'字段列表'在   com.mysql.jdbc.SQLError.createSQLException(SQLError.java:936)at at   com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2985)at at   com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1631)at at   com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1723)at at   com.mysql.jdbc.Connection.execSQL(Connection.java:3250)at   com.mysql.jdbc.Statement.executeUpdate(Statement.java:1355)at   com.mysql.jdbc.Statement.executeUpdate(Statement.java:1270)at   org.apache.jsp.reg_jsp._jspService(reg_jsp.java:157)at   org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)at   javax.servlet.http.HttpServlet.service(HttpServlet.java:729)at at   org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:438)     在   org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396)     在org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340)     在javax.servlet.http.HttpServlet.service(HttpServlet.java:729)at   org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)     在   org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)     在   org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)     在   org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)     在   org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)     在   org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:217)     在   org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)     在   org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)     在   org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)     在   org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)     在   org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)     在   org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)     在   org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)     在   org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)     在   org.apache.coyote.AbstractProtocol $ AbstractConnectionHandler.process(AbstractProtocol.java:673)     在   org.apache.tomcat.util.net.NioEndpoint $ SocketProcessor.doRun(NioEndpoint.java:1500)     在   org.apache.tomcat.util.net.NioEndpoint $ SocketProcessor.run(NioEndpoint.java:1456)     at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)     at java.util.concurrent.ThreadPoolExecutor $ Worker.run(Unknown Source)     在   org.apache.tomcat.util.threads.TaskThread $ WrappingRunnable.run(TaskThread.java:61)     在java.lang.Thread.run(未知来源)没有   insertedcom.mysql.jdbc.exceptions.MySQLSyntaxErrorException:未知   栏'性别'在'字段列表'

从这段代码:

String fname=request.getParameter("firstnm"); 
String lnames=request.getParameter("lname"); 
String gen=request.getParameter("ddlGender"); 
String mail=request.getParameter("txtMailID"); 
String mobile=request.getParameter("txtMobilenumber"); 
String pass1=request.getParameter("txtPass"); 
String pass2=request.getParameter("txtPass1"); 
Integer tim=Integer.parseInt(mobile); 
Integer passw=Integer.parseInt(pass1); 
Integer passw2=Integer.parseInt(pass2);

String query="INSERT INTO register (firstname, lastname, gender, email, mobile, password1, password2) VALUES('"+fname+"','"+lnames+"','"+gen+"','"+mail+"','"+tim+‌​"','"+passw+"','"+pa‌​ssw2+"')"; 
try { 
    Class.forName("com.mysql.jdbc.Driver").newInstance();
    cons=DriverManager.getConnection(conurl,conuname,conpass);
    stmt=cons.createStatement();
    stmt.executeUpdate(query);
    System.out.println("inserted");
    cons.close(); 
} catch(Exception ex){ 
    ex.printStackTrace(); 
    System.out.println("not inserted"+ex); 
}

1 个答案:

答案 0 :(得分:0)

您的代码中似乎存在一个小问题 您不能使用"+VARIABLE_NAME+"在数据库中插入Integer数据类型,它仅适用于String数据类型。而是插入Integer drop单个倒置引号并仅使用"+VARIABLE_NAME+"

将您的SQL查询修改为,

String query="INSERT INTO register (firstname, lastname, gender, email, mobile, password1, password2) 
   VALUES('"+fname+"','"+lnames+"','"+gen+"','"+mail+"',"+tim+‌​","+passw+","+pa‌​ssw2+")";