我試圖從jtable和數據庫一起刪除選定的行,但我收到語法錯誤。可能是因爲我使用varchar或真的在這種情況下語法是錯誤的?嘗試從jtable刪除Java SQL語法錯誤
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
我還建議你看一下'PreparedStatement's – MadProgrammer