SQL Update Maria DB with Prepared Statement

2019-07-28 11:58发布

问题:

Please help.. I have search it. But I still don't know where is my fault. Maybe I just miss something.

Here is the error :

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '?, ID_JABATAN=?, TANGGAL_MASUK=?, TANGGAL_KELUAR=?, ID_JENIS_KARYAWAN=? WHERE ID' at line 1

And this is my code :

    try {
        DBConnection knk = new DBConnection();
        Connection conn = knk.bukaKoneksi();
        String sql = "UPDATE KARYAWAN SET NAMA_KARYAWAN=?, ID_JABATAN=?, TANGGAL_MASUK=?, TANGGAL_KELUAR=?, ID_JENIS_KARYAWAN=? WHERE ID_KARYAWAN=?";
        PreparedStatement ps = conn.prepareStatement(sql);
        ps.setString(1, karyawan.getNamaKaryawan());
        ps.setInt(2, karyawan.getIdJabatan());
        ps.setDate(3, karyawan.getTanggalMasuk());
        ps.setDate(4, karyawan.getTanggalKeluar());
        ps.setInt(5, karyawan.getIdJenisKaryawan());
        ps.setInt(6, karyawan.getIdKaryawan());

        int hasil = ps.executeUpdate(sql);
        return hasil > 0;
    } catch (SQLException e) {
        e.printStackTrace();
        return false;
    }

And the column of the table :

回答1:

Try this:

int hasil = ps.executeUpdate();


回答2:

remove the Parameter from int hasil = ps.executeUpdate(sql);

if you call it with Parameter, the query will be executet, not the prepared Statement.

See the javadoc:

int executeUpdate(String sql) throws SQLException

Executes the given SQL statement, which may be an INSERT, UPDATE, or DELETE statement or an SQL statement that returns nothing, such as an SQL DDL statement. Note:This method cannot be called on a PreparedStatement or CallableStatement. Parameters:sql - an SQL Data Manipulation Language (DML) statement, such as INSERT, UPDATE or DELETE; or an SQL statement that returns nothing, such as a DDL statement.Returns:either (1) the row count for SQL Data Manipulation Language (DML) statements or (2) 0 for SQL statements that return nothingThrows:SQLException - if a database access error occurs, this method is called on a closed Statement, the given SQL statement produces a ResultSet object, the method is called on a PreparedStatement or CallableStatementSQLTimeoutException - when the driver has determined that the timeout value that was specified by the setQueryTimeout method has been exceeded and has at least attempted to cancel the currently running Statement


int executeUpdate() throws SQLException Executes the SQL statement in this PreparedStatement object, which must be an SQL Data Manipulation Language (DML) statement, such as INSERT, UPDATE or DELETE; or an SQL statement that returns nothing, such as a DDL statement. Returns:either (1) the row count for SQL Data Manipulation Language (DML) statements or (2) 0 for SQL statements that return nothingThrows:SQLException - if a database access error occurs; this method is called on a closed PreparedStatement or the SQL statement returns a ResultSet objectSQLTimeoutException - when the driver has determined that the timeout value that was specified by the setQueryTimeout method has been exceeded and has at least attempted to cancel the currently running Statement