11
回答
mysql 一点小问题
【腾讯云】校园拼团福利,1核2G服务器10元/月!>>>   

JDBC实现的一个保存用户的方法:

public void saveUser(String username, String name, String pwd, String age,
   String sex,String phone) throws Exception {
       StringBuffer sb = new StringBuffer();
       sb.append("insert into friend_user(username,");
       sb.append("password,name,age,gendar,phone)  ");
       sb.append("values(?,?,?,?,?,?)");
       String sql = sb.toString();
       Connection con = null;
       con = DBUtil.openInThread();
       PreparedStatement prep = con.prepareStatement(sql);
       prep.setString(1,username);
       prep.setString(2,name);
       prep.setString(3,pwd);
       prep.setInt(4,Integer.parseInt(age));
       int s;
        if("m".equals("sex")){
              s =1;
        }else{
              s =0;
        }
        prep.setInt(5, s);
        prep.setString(6, phone);
        prep.executeUpdate(sql);
        DBUtil.closeInThread();
        prep.close();
    }

mysql驱动包是:mysql-connector-java-5.1.8-bin.jar

现在运行结果出现异常:

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '?,?,?,?,?,?)' at line 1
 at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
 at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
 at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
 at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
 at com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
 at com.mysql.jdbc.Util.getInstance(Util.java:381)
 at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1030)
 at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)
 at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3536)
 at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3468)
 at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1957)
 at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2107)
 at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2642)
 at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1634)
 at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1553)
 at org.apache.commons.dbcp.DelegatingStatement.executeUpdate(DelegatingStatement.java:228)
 at UserDaoImpl.UserDaoJDBCImpl.saveUser(UserDaoJDBCImpl.java:58)
 at web.UserMsgServlet.service(UserMsgServlet.java:32)
 at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
 at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
 at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164)
 at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462)
 at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
 at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
 at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:562)
 at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
 at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:395)
 at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:250)
 at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:188)
 at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302)
 at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
 at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
 at java.lang.Thread.run(Thread.java:619)

什么问题 求分析????
举报
kong_resty
发帖于6年前 11回/1K+阅
共有11个答案 最后回答: 6年前

引用来自“红薯”的答案

SQL 语法错误

我仔细看了 values附近没错误啊!

--- 共有 1 条评论 ---
红薯没错误的话mysql是不会报语法错误的,认真检查哦 6年前 回复

引用来自“章彦儿”的答案

引用来自“wgzh159”的答案

至少,我看到一个单词写错了....gender

.....这个有关系吗

你看看你的数据库表有没有这个字段?

引用来自“wgzh159”的答案

引用来自“章彦儿”的答案

引用来自“wgzh159”的答案

至少,我看到一个单词写错了....gender

.....这个有关系吗

你看看你的数据库表有没有这个字段?
顶部