Post无法将数据插入MySQL表,列数与第1行

时间:2018-02-06 00:46:29

标签: java mysql

无法使用post方法将数据插入MySql数据库,得到错误java.sql.SQLException:列数与第1行的值计数不匹配 下面是我的INSERT语句,它之前工作正常,但添加两个新列后得到上述错误。 以下是两个新列的详细信息:PLAYER_STATUS,IsAdmin

PLAYER_STATUS varchar(250)NO Active - 此列不是NULL,默认值为' Active'
    IsAdmin tinyint(1)NO 0 - 此布尔列不为NULL,默认值为0

PreparedStatement ps = conn.prepareStatement(
                                        "INSERT INTO mycoolmap.weekendsoccer_login values(default,?,?,?,?,?,?,?)",
                                        Statement.RETURN_GENERATED_KEYS);
                        ps.setString(1, p_Name);
                        ps.setString(2, p_Email);
                        ps.setString(3, p_Mobile);                      
                        ps.setString(4, encrptPass);                
                        ps.setString(5, p_Company);         
                        ps.setString(6, "Active");          // newly added column
                        ps.setInt(7, 0);                    // newly added column

                        x = ps.executeUpdate();

**//Error details below:**
java.sql.SQLException: Column count doesn't match value count at row 1
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:964)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3970)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3906)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2524)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2677)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2549)
    at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1861)
    at com.mysql.jdbc.PreparedStatement.executeUpdateInternal(PreparedStatement.java:2073)
    at com.mysql.jdbc.PreparedStatement.executeUpdateInternal(PreparedStatement.java:2009)
    at com.mysql.jdbc.PreparedStatement.executeLargeUpdate(PreparedStatement.java:5098)
    at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1994)
    at com.myfirstjavatest.pkg.JSONService.createPlayerInJSON(JSONService.java:186)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.glassfish.jersey.server.model.internal.ResourceMethodInvocationHandlerFactory$1.invoke(ResourceMethodInvocationHandlerFactory.java:81)
    at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher$1.run(AbstractJavaResourceMethodDispatcher.java:144)
    at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.invoke(AbstractJavaResourceMethodDispatcher.java:161)
    at org.glassfish.jersey.server.model.internal.JavaResourceMethodDispatcherProvider$ResponseOutInvoker.doDispatch(JavaResourceMethodDispatcherProvider.java:160)
    at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.dispatch(AbstractJavaResourceMethodDispatcher.java:99)
    at org.glassfish.jersey.server.model.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:389)
    at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:347)
    at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:102)
    at org.glassfish.jersey.server.ServerRuntime$2.run(ServerRuntime.java:326)
    at org.glassfish.jersey.internal.Errors$1.call(Errors.java:271)
    at org.glassfish.jersey.internal.Errors$1.call(Errors.java:267)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:267)
    at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:317)
    at org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:305)

1 个答案:

答案 0 :(得分:0)

  1. 正如@Scary指出的那样。您应该指定字段名称,例如insert into table_name(field_1, field_2) value (?, ?)
  2. 如果您需要为行设置默认值,并且您具有通过MySQL控制器的角色。你应该在SQL中做到这一点,更安全(但不太灵活)。 e.g。
  3. CREATE TABLE weekendsoccer_login(
       id INT(11) NOT NULL UNIQUE AUTO_INCREMENT,
       ...
       status varchar(255) NOT NULL DEFAULT "Active",
       ...
       PRIMARY KEY (id)
    );
    
    1. 为了学习JDBC,我确定你做得很好。但是如果你真的想去制作,你应该看看JPAHibernateEclipseLink实施)或jOOQ(我喜欢这个,非常方便,节省了很多时间)