java.sql.SQLException:列数与第 1 行的值数不匹配 jdbc

java.sql.SQLException: Column count doesn't match value count at row 1 jdbc

Connection conn = null;

PreparedStatement pst = null;
try {
    conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/chatdata",      "admin", "int*M=9167757203");
    pst = conn.prepareStatement("insert into user values (?, ?)"); 
} catch (Exception e) {

        } 
String password;
String userName;
try {
    BufferedReader kin = new BufferedReader(new InputStreamReader(System.in));
    System.out.println("Enter Username :: ");
    userName = kin.readLine();
    System.out.println("Enter Password :: ");
    password = kin.readLine();
    pst.setString(1, userName);
    pst.setString(2, password);
    pst.execute();
} catch (Exception e) {
    System.out.println(e);
}

这段代码向我展示了错误:

"java.sql.SQLException: Column count doesn't match value count at row 1"

虽然我的 table 有三列条目 uid,即 AUTO_INCREMENT 和用户名和密码,这是我必须传递的两个字符串,所以如果有人能找到答案,请帮忙。

这就是 MySQL 手册关于使用不带列列表的 insert 的内容:

If you do not specify a list of column names for INSERT ... VALUES or INSERT ... SELECT, values for every column in the table must be provided by the VALUES list or the SELECT statement.

因此,即使 uid 具有 autoincrement 标志,您仍然需要在插入语句中指定要为哪些列提供值。作为解决方案,您可以简单地修改插入语句:

insert into user (username, password) values (?, ?)

(我认为 usernamepassword 是列的名称。)

您可以阅读有关使用自动增量的更多信息here

netbeans 在 mysql cmd 中以不同的方式评估字段,如果您键入此语法说 插入用户 ('username', 'password') 值 ('xyz', 'abc'); 它可以很好地工作,但在 netbeans 中,表和字段名称不是用单引号而是用``这种类型的引号表示的,所以这在 netbeans 中非常好 插入 user (username, password) 值 ('xyz', 'abc'); 其中 xyz 和 abc 等字符串用单引号表示...