列数与第1行的值计数不匹配[Java]

时间:2017-09-03 04:11:35

标签: java mysql eclipse wamp

我正在尝试将音乐CD信息添加到带有应用程序的数据库中。单击该按钮时,它会将信息写入WAMP DB。我一直收到错误,我看了,找不到问题的解决方案。

Driver loaded
SQL error during INSERT
java.sql.SQLException: Column count doesn't match value count at row 1
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:946)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2985)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1631)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1723)

这些是我得到的错误

@Override
public void actionPerformed(ActionEvent e) { //this runs when the submit button is pressed
    String artistName = txtName.getText();
    ...
    String format = "";
    if (rdbtnCd.isSelected()) {
        format = "CD";
    } else if (rdbtnVinyl.isSelected()) {
        format = "Vinyl";
    }

    String query = "INSERT INTO cds VALUES ('" + artistName + "','" + albumTitle + "','" + recordLabel + "','" + genre + "','" + year + "','" + format +"');";

    try {
        //  Connection c = DBClass.getConnection();
        Statement stmt = conn.createStatement();
        stmt.executeUpdate(query);
        JOptionPane.showMessageDialog(this, "Saved");
    } catch (SQLException e1) {
        System.out.println("SQL error during INSERT");
        e1.printStackTrace();
    }

这是我的连接

private void connect() {
    try { //Login details for the DB.
        String userName = "root";
        String password = "";
        String url = "jdbc:mysql://localhost/music";
        conn = DriverManager.getConnection(url, userName, password);
    } catch (Exception e) { //Error is DB connection is unsuccessful .
        JOptionPane.showMessageDialog(null, "Cannot connect to database server");
        // System.err.println(e.getMessage());
        System.err.println(e.getStackTrace());
    }
    JOptionPane.showMessageDialog(null, "Database connection established");

}

任何帮助将不胜感激。谢谢

1 个答案:

答案 0 :(得分:0)

错误意味着您提供的数据不像表cds包含列那样多。现在数据库引擎不知道在哪些列中放置数据。

要解决此问题,您必须提供要填充的列的名称。例如:

insert into cds(column_name1, column_name2)
values (1, 3)    

查找表定义并查看要填充的列。

插入表示您正在插入新记录。您没有修改现有的。使用更新。