I add the same code ( INSERT ) record and the path remains with backslashes. Note: C:\Users\COMP\Desktop\Kartinka.png , but when, for example, I want to change ( UPDATE ) this path through the same code, it replaces me, but removes backslashes. Note: C:UsersCOMPDesktopKartinka.png

What is the problem and how to fix it?

 JFileChooser chooser=new JFileChooser(); chooser.showOpenDialog(null); File f=chooser.getSelectedFile(); String filename=f.getAbsolutePath(); jTextField1.setText(filename); ImageIcon icon = new ImageIcon(f.getAbsolutePath()); 

Request to update the record:

 query = "UPDATE battle SET name = '"+jTextField2.getText()+"', Image = '"+jTextField1.getText()+"' WHERE name = '"+jTextField2.getText()+"' ;"; 
  • one
    Give the code to add / update data in the database. - Victor Khovanskiy
  • query = "UPDATE battle SET name = '"+jTextField2.getText()+"', Image = '"+jTextField1.getText()+"' WHERE name = '"+jTextField2.getText()+"' ;"; - Bogdan Oryschych
  • On adding, I did a little differently, but having done the same for updating, the essence has not changed. protected void insertW(String s1, String s2) query = "INSERT INTO db.work (db.work .name, db.work .Image) VALUES (?,?);"; and through insertW I transfer the value from the field, insertW(jTextField1.getText(), jTextField2.getText()); - Bogdan Oryschych
  • Use that method and update. query = "UPDATE battle SET name = ?, Image = ? WHERE name = ? ;"; - Victor Khovanskiy

1 answer 1

Escape the \ character, that is, replace it with \\ . Then the path will be as follows C:\\Users\\COMP\\Desktop\\Kartinka.png . Better yet, replace with / , as they are universal and supported on Windows.

Note: use placeholder ? when updating data.

 query = "UPDATE battle SET name = ?, Image = ? WHERE name = ? ;";