简体   繁体   English

使用预准备语句的SQL Update Maria DB

[英]SQL Update Maria DB with Prepared Statement

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 : 和表的列: 表格栏

remove the Parameter from int hasil = ps.executeUpdate(sql); int hasil = ps.executeUpdate(sql);删除参数int hasil = ps.executeUpdate(sql);

if you call it with Parameter, the query will be executet, not the prepared Statement. 如果使用Parameter调用,查询将被执行,而不是准备好的Statement。

See the javadoc : 参见javadoc

int executeUpdate(String sql) throws SQLException int executeUpdate(String sql)抛出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. 执行给定的SQL语句,该语句可以是INSERT,UPDATE或DELETE语句,也可以是不返回任何内容的SQL语句,例如SQL DDL语句。 Note:This method cannot be called on a PreparedStatement or CallableStatement. 注意:不能在PreparedStatement或CallableStatement上调用此方法。 Parameters:sql - an SQL Data Manipulation Language (DML) statement, such as INSERT, UPDATE or DELETE; 参数:sql-SQL数据操作语言(DML)语句,例如INSERT,UPDATE或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 返回值:(1)SQL数据操作语言(DML)语句的行计数或(2)不返回任何值的SQL语句的行计数Throws:SQLException-如果访问数据库发生错误,在封闭的Statement上调用此方法,给定的SQL语句生成ResultSet对象,在PreparedStatement或CallableStatementSQLTimeoutException上调用此方法-当驱动程序确定已超过setQueryTimeout方法指定的超时值时并且至少已尝试取消当前正在运行的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; int executeUpdate()引发SQLException在此PreparedStatement对象中执行SQL语句,该对象必须是SQL数据操作语言(DML)语句,例如INSERT,UPDATE或DELETE; or an SQL statement that returns nothing, such as a DDL statement. 或不返回任何内容的SQL语句,例如DDL语句。 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; 返回:(1)SQL数据操作语言(DML)语句的行数,或者(2)不返回任何内容的SQL语句的行数throws:SQLException-如果发生数据库访问错误; 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 在关闭的PreparedStatement上调用此方法,或者SQL语句返回ResultSet objectSQLTimeoutException-当驱动程序确定已超过setQueryTimeout方法指定的超时值并且至少已尝试取消当前运行的Statement时

尝试这个:

int hasil = ps.executeUpdate();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM