尝试执行 INSERT 查询时出现 SQLException "discrepancy in the number of rows counted"

SQLException "discrepancy in the number of rows counted" when trying to execute an INSERT query

我正在尝试将一些值插入到 table 中,但是 Java 控制台中出现 SQLException,显示 "discrepancy in the number of rows counted".

代码如下

public static int enviaMensaje(int id_destinatario, String mensaje) throws Exception{
    PreparedStatement ps = null;        
    Connection con = null;

    int id_insertado = 0;

    try {

        String SQL_DRV = "org.hsqldb.jdbcDriver";
        String SQL_URL = "jdbc:hsqldb:hsql://localhost/";

        Class.forName(SQL_DRV);
        con = DriverManager.getConnection(SQL_URL, "sa", "");
        ps = con.prepareStatement("insert into public.mensaje values(?, ?)",
                Statement.RETURN_GENERATED_KEYS);

        ps.setInt(1, id_destinatario);
        ps.setString(2, mensaje);

        int updated = ps.executeUpdate();
        if (updated != 1) {
            throw new Exception("[ALREADY PERSISTED]");
        } 

        if (updated == 1) {
            ResultSet generatedKeys = ps.getGeneratedKeys();
            if (generatedKeys.next()) {
                id_insertado = generatedKeys.getInt(1);
            }
        }

    } catch (ClassNotFoundException e) {
        e.printStackTrace();
        try {
            throw new Exception("Driver not found", e);
        } catch (Exception e1) {
            e1.printStackTrace();
        }
    } catch (SQLException e) {
        e.printStackTrace();
        try {
            throw new Exception("Invalid SQL or database schema", e);
        } catch (Exception e1) {
            e1.printStackTrace();
        }
    } finally {
        if (ps != null) {
            try {
                ps.close();
            } catch (Exception ex) {
            }
        }
        ;
        if (con != null) {
            try {
                con.close();
            } catch (Exception ex) {
            }
        }
        ;
    }
    return id_insertado;
}

table 有列... ID、ID_RECEIVER 和内容。 ID 是带有 "IDENTITY" 关键字的主键,因此只要将记录插入到 table.

中,它就会自动递增

您需要指定列,因为您没有为所有列提供值。

 ps = con.prepareStatement("insert into public.mensaje(id_receiver, content) values(?, ?)",
                Statement.RETURN_GENERATED_KEYS);