Skip to content
Advertisement

How can I delete or update a record sql from JTable in Java Swing

I’m coding on a java Swing sql project. I’m stuck at deleting and updating an information record. I can add a new record normally but can’t update or delete one. When I delete, it will notify ‘can’t find column number’. Here is my code and a picture of the error message. Thanks in advance.

enter image description here

Update button

Advertisement

Answer

You are using an invalid SQL-statement. The line you are writing (and sending to the database) is DELETE FROM Student where RollNo = A02.

What you expect (in valid SQL) would be: DELETE FROM Student where RollNo = 'A02'.

Now for simplicity, you could use

But that would be open to SQL injection, meaning that if the String in the variable cell could read something like "'; drop table student;", your database would drop the students table.

Better use the prepared statement (https://docs.oracle.com/javase/7/docs/api/java/sql/PreparedStatement.html):

This way, the String is properly escaped. Please refere to jdbc’s documentation for details.

PreparedStatement – https://docs.oracle.com/javase/7/docs/api/java/sql/PreparedStatement.html Connection – https://docs.oracle.com/javase/7/docs/api/java/sql/Connection.html#prepareStatement(java.lang.String)

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement