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

标签 mysql 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"

虽然我的表有三列条目 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/

相关文章:

java - 结果集到数组

php - 通过php连接mysql

php - 使用 PHP 查询 MySQL

php - 将数据库数据加载为动态页面

java - 我在使用 netbeans 时收到此错误 pls 00103

multithreading - 如何管理服务器上的数据库连接?

mysql - 从控制台将sql输入到批处理脚本

php - jQuery AJAX 添加/编辑/删除操作可以被黑客攻击吗?

java - 如何在java中将文本字段中的值插入数据库

mysql - 苦苦挣扎-在JAVA中找不到类异常: com. mysql.jdbc.Driver