SQL 使用准备好的语句更新 Maria DB
SQL Update Maria DB with Prepared Statement
请帮忙..我已经搜索过了。但我仍然不知道我的错在哪里。也许我只是错过了什么。
这是错误:
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
这是我的代码:
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;
}
以及 table 的列:
从 int hasil = ps.executeUpdate(sql);
中删除参数
如果你用参数调用它,查询将被执行,而不是准备好的语句。
参见 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
试试这个:
int hasil = ps.executeUpdate();
请帮忙..我已经搜索过了。但我仍然不知道我的错在哪里。也许我只是错过了什么。
这是错误:
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
这是我的代码:
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;
}
以及 table 的列:
从 int hasil = ps.executeUpdate(sql);
如果你用参数调用它,查询将被执行,而不是准备好的语句。
参见 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
试试这个:
int hasil = ps.executeUpdate();