简体   繁体   中英

Java SQL syntax error trying to delete from jtable

i'm trying to delete selected row from jtable and database together but i'm getting syntax error. Might be cause i'm using varchar or really the syntax is just wrong in this case?

    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");
            }

ERROR:

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

Database variables:

Kods -Index -int(255)       
Nosaukums - varchar(255)         
Inventara Nr - varchar(255)  
Uzskaites vertiba - float                
Iegades vertiba - float

Note that your columns have space-separated names. Normally it isn't best-practice to use such a naming convention, but snake_case or CamelCase.

In your case, you need to properly quote your column names, eg

WHERE `Inventara Nr` = '33'

Be aware though that your code is vulnerable to SQL injections. I recommend reading about them. You should always use PreparedStatements .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM