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"
虽然我的表有三列条目 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 (?, ?)
(我认为用户名
和密码
是列的名称。)
您可以阅读有关使用自动增量的更多信息 here
关于mysql - java.sql.SQLException : Column count doesn't match value count at row 1 jdbc,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28255014/