user8775227
user8775227

Reputation:

update query syntax error in java

im trying to make an update query on my java, i keep getting error in my sql syntax but i already tried executing the query in my sqlyog and it worked, but it doesnt work in my program, here is my query

String query = "UPDATE t_surat_masuk SET kode_klasifikasi = '"+kode_klasifikasi+"',"

                        + " pengirim = '"+pengirim+"',"
                        + " no_surat = '"+no_surat+"', "
                        + " tgl_surat = '"+tanggalsurat+"',"
                        + " perihal = '"+perihal+"',"
                        + " tgl_surat_masuk = '"+tanggalmasuk+"', "
                        + " penerima = '"+penerima+"', "
                        + " WHERE id_surat='"+id_surat+"'";

sorry for my bad english, thank you

Upvotes: 0

Views: 55

Answers (1)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 521249

Your exact syntax error is that you have a stray comma after the SET clause, before the WHERE clause. But the best answer here is for you to use a prepared statement:

String sql = "UPDATE t_surat_masuk SET kode_klasifikasi = ?, pengirim = ?, ";
sql += "no_surat = ?, tgl_surat = ?, perihal = ?, tgl_surat_masuk = ?, penerima = ? ";
sql += "WHERE id_surat = ?";
PreparedStatement ps = con.prepareStatement(sql);
ps.setString(1, kode_klasifikasi);
ps.setString(2, pengirim);
ps.setString(3, no_surat);
ps.setString(4, tanggalsurat);
ps.setString(5, perihal);
ps.setString(6, tanggalmasuk);
ps.setString(7, penerima);
ps.setInt(8, id_surat);
ps.executeUpdate();

Note that I assumed all columns are strings, except for the id_surat column, which sounds like an integer column. You may have to change the types of some of the above setters to get it to work.

In general, you can see that with a prepared statement, you may write out what is essentially the actual raw query. This makes it much harder to have syntax errors of the sort you currently have.

Upvotes: 1

Related Questions