更新java中的查询语法错误

时间:2023-02-09 01:35:46

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

我试图在我的java上进行更新查询,我一直在我的sql语法中得到错误但我已经尝试在我的sqlyog中执行查询并且它有效,但它在我的程序中不起作用,这是我的查询

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

抱歉我的英文不好,谢谢

1 个解决方案

#1


2  

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:

您确切的语法错误是在WHERE子句之前的SET子句之后有一个逗号逗号。但这里最好的答案是你使用一个准备好的声明:

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.

请注意,我假设所有列都是字符串,除了id_surat列,它听起来像一个整数列。您可能必须更改上述某些setter的类型才能使其工作。

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.

通常,您可以看到使用预准备语句,您可以写出实质上是原始查询的内容。这使得您目前拥有的语法错误变得更加困难。

#1


2  

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:

您确切的语法错误是在WHERE子句之前的SET子句之后有一个逗号逗号。但这里最好的答案是你使用一个准备好的声明:

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.

请注意,我假设所有列都是字符串,除了id_surat列,它听起来像一个整数列。您可能必须更改上述某些setter的类型才能使其工作。

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.

通常,您可以看到使用预准备语句,您可以写出实质上是原始查询的内容。这使得您目前拥有的语法错误变得更加困难。