lakshi lakshi - 1 year ago 74
MySQL Question

How to update file path with \\ in mysql using java?

After updating the attachement field with new file, mysql file path cosists with no slashs. Could anyone show me the path to fix it?

This is my file attachment button:

private void attachActionPerformed(java.awt.event.ActionEvent evt){
JFileChooser chooser=new JFileChooser();
File f=chooser.getSelectedFile();
String file=f.getAbsolutePath();

This is my update button code:

private void update_fieldsActionPerformed(java.awt.event.ActionEvent evt){
String add1=course_catergory.getSelectedItem().toString();
String add2=code_course.getText();
String add3=course_type.getSelectedItem().toString();
String add4=course_name.getText();
String add5=file_attach.getText();

String sql="UPDATE course SET category='"+add1+"' ,course_code='"+add2+"' ,course_type='"+add3+
"' ,course_name='"+add4+"' ,attach_file='"+add5+"' where course_code='"+add2+"' ";

JOptionPane.showMessageDialog(null, "Successfully updated.");

catch(Exception e){
JOptionPane.showMessageDialog(null, e);

Answer Source

Use PreparedStatement with ? not like the native query, this is an exemple how to use it correctly:

try {
    String sql = "UPDATE course SET category = ?, course_code = ? ,course_type = ? ,course_name = ?, "
            + "attach_file=? where course_code = ? ";

    pst = conn.prepareStatement(sql);
    pst.setString(1, add1);
    pst.setString(2, add2);
    pst.setString(3, add3);
    pst.setString(4, add4);
    pst.setString(5, add5);
    pst.setString(6, add2);
    pst.executeUpdate JOptionPane.showMessageDialog(null, "Successfully updated.");

} catch (Exception e) {
    JOptionPane.showMessageDialog(null, e);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download