简体   繁体   English

程序中“列计数与第1行的值计数不匹配”错误

[英]“Column count doesn't match value count at row 1” error in program

I am inserting values into a database but get a "column count doesnt match value count value at row1" error. 我正在将值插入数据库,但得到“列数与row1处的值计数值不匹配”错误。

try {
    Class.forName("com.mysql.jdbc.Driver");
    connection = DriverManager.getConnection(connectionURL, "root", "root");
    String sql = "insert into login(username,password) values(?,?)";
    PreparedStatement pst = connection.prepareStatement(sql);
    pst.setString(1, username);
    pst.setString(2, password);

    int numRowsChanged = pst.executeUpdate();
    out.println(" Data has been submitted ");

    pst.close();
} catch (ClassNotFoundException e) {
    out.println("Couldn't load database driver: " + e.getMessage());
} catch (SQLException e) {
    out.println("SQLException caught: " + e.getMessage());
} catch (Exception e) {
    out.println(e);
} finally {
    try {
        if (connection != null)
            connection.close();
    } catch (SQLException ignored) {
        out.println(ignored);
    }
}

This my table in MySql database: 这是我在MySql数据库中的表:

           +----------+-------------+------+-----+---------+-------+
           | Field    | Type        | Null | Key | Default | Extra |
           +----------+-------------+------+-----+---------+-------+
           | username | varchar(20) | YES  |     | NULL    |       |
           | password | varchar(15) | YES  |     | NULL    |       |
           +----------+-------------+------+-----+---------+-------+

Why am I getting this error? 为什么我收到此错误? Is there something wrong in my code? 我的代码中有什么问题吗? Please help me....... 请帮我.......

也许在sql String中列的名称不正确。

The error is itself erroneous probably, as everything looks fine. 错误本身可能是错误的,因为一切看起来都很好。 Maybe the primary key of the table login is not defined ( username )? 也许表登录的主键没有定义( 用户名 )? I see username is nullable. 我看到用户名是可以为空的。

Another approach, a non-prepared statement might give an idea. 另一种方法,一种未经准备的陈述可能会给出一个想法。

String sql = "insert into login(username, password) values('"
        + username + "', '" + password + "')";
System.out.println("sql = " + sql);
Statement pst = connection.createStatement();
pst.execute(sql);

If nothing helps, create a new table xxx and try all on that. 如果没有任何帮助,请创建一个新表xxx并尝试全部。

Actually, this error is caused when the number of values supplied and the number columns does not match. 实际上,当提供的值数和列数不匹配时会导致此错误。 (ie) insert into table (col1,col2) values (val1) and insert into table (col1) values (val1,val2) both would generate same error. (即)插入表(​​col1,col2)值(val1)并插入表(col1)值(val1,val2)都会产生相同的错误。

I tried your code and it is perfectly working with Oracle. 我尝试了你的代码,它完全适用于Oracle。 So, you can check for some spelling mistakes in names of the columns. 因此,您可以检查列名称中的一些拼写错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM