值java.sql.SQLException:列数并不在行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:列数并不在行1 JDBC
"java.sql.SQLException: Column count doesn't match value count at row 1"
虽然我的表有三列条目UID是AUTO_INCREMENT
和用户名和密码两个字符串,我必须通过所以请如果有人可以找到答案请帮助。
netbeans以不同的方式在mysql cmd中评估字段,如果你输入这个语法,说 insert into user('username','password')values('xyz','abc'); 它会工作得很好,但在netbeans中,表和字段名称不是用单引号表示,而是在这种类型的引号中,因此netbeans 插入user
(username
,password
)values('xyz', 'ABC'); 其中像xyz和abc这样的字符串用单引号表示...
这是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语句中指定你目前列值。作为一个解决方案,你可以简单地修改你的插入语句:
insert into user (username, password) values (?, ?)
(我假定username
和password
是列的名称。)
你可以阅读更多有关使用自动增量here