Delete in JTable with connect SQL?

I want to delete data whole row i choose when i press in the Id’s column , so i try with this code but i have this error : "java.sql.SQLException: Conversion failed when converting the nvarchar value ‘T123 ‘ to data type int. " ‘T123’ is a Id’s data

 if (e.getSource() == delete) //button delete {         int ret = JOptionPane.showConfirmDialog(this, "Do you want to delete?", "Confirm", JOptionPane.YES_NO_OPTION);         if (ret != JOptionPane.YES_OPTION) {             return;         }         Connection c = null;         PreparedStatement ps = null;         String url = "net.sourceforge.jtds.jdbc.Driver";         int id = tb.getSelectedRow();         try {             Class.forName(url);             String db = "jdbc:jtds:sqlserver://DUCTHANG:1433/student";             c = DriverManager.getConnection(db, "username", "password");             ps = c.prepareStatement("Delete From info where ID = ?");             ps.setInt(1, id); //first column's value was choose in table             ret = ps.executeUpdate();             if (ret != -1) {                 JOptionPane.showMessageDialog(this, "This Student has been deleted");             }         } catch (Exception ex) {             ex.printStackTrace();         } finally {             try {                 if (c != null) {                     c.close();                 }                 if (ps != null) {                     ps.close();                 }             } catch (Exception ex2) {                 ex2.printStackTrace();             }         }     } 
Add Comment
1 Answer(s)

Based on what you described, your database table’s ID column is NVCHAR type but you are setting it to int. Another point to note here is, you need to get the ROW ID based on the selected row ID and then use setString on your ps statement.

int id = tb.getSelectedRow(); // Now, get respective DB Row ID into String idString and then use the following // statement in place of ps.setInt(1, id); ps.setString(1, idString); 
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.