[英]Java SQL syntax error trying to delete from jtable
我試圖一起從jtable和數據庫中刪除選定的行,但是我遇到語法錯誤。 可能是因為我使用的是varchar還是在這種情況下語法實際上是錯誤的?
btnNewButton_2 = new JButton("Dzēst");
btnNewButton_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Statement statement = null;
try {
int tableRow = table_1.getSelectedRow();
Object Kods = table_1.getValueAt(tableRow, 0);
Object Nosaukums = table_1.getValueAt(tableRow, 1);
Object Inventara = table_1.getValueAt(tableRow, 2);
Object Uzskaites = table_1.getValueAt(tableRow, 3);
Object Iegad = table_1.getValueAt(tableRow, 4);
Statement stmt = null;
Connection connection = ConnectDB();
String sql = "DELETE FROM users " +
"WHERE Kods = " + Kods + " AND Nosaukums = '" + Nosaukums + "' AND Inventara Nr = " +
Inventara + " AND Uzskaites vertiba = '" + Uzskaites + "' AND Iegades vertiba = " + Iegad;
stmt = connection.createStatement();
stmt.executeUpdate(sql);
} catch (SQLException ex) {
Logger.getLogger(dddddddd.class.getName()).log(Level.SEVERE, null, ex);
JOptionPane.showMessageDialog(null, "Nav ievadita visa informacija");
}
錯誤:
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 'Nr = ttttt AND Uzskaites vertiba = '3.0' AND Iegades vertiba = 3.0' at line 1
數據庫變量:
Kods -Index -int(255)
Nosaukums - varchar(255)
Inventara Nr - varchar(255)
Uzskaites vertiba - float
Iegades vertiba - float
請注意,您的列具有以空格分隔的名稱。 通常,使用這種命名約定不是最佳實踐,但是使用snake_case或CamelCase。
對於您的情況,您需要正確引用您的列名,例如
WHERE `Inventara Nr` = '33'
請注意,盡管您的代碼容易受到SQL注入的攻擊。 我建議閱讀有關它們的文章。 您應該始終使用PreparedStatements 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.