1

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();
    chooser.showOpenDialog(null);
    File f=chooser.getSelectedFile();
    String file=f.getAbsolutePath();
    file_attach.setText(file); 
}

This is my update button code:

private void update_fieldsActionPerformed(java.awt.event.ActionEvent evt){  
    try{ 
        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+"' ";

        pst=conn.prepareStatement(sql);
        pst.executeUpdate();
        JOptionPane.showMessageDialog(null, "Successfully updated.");

    }
    catch(Exception e){
        JOptionPane.showMessageDialog(null, e);
    }
    update_course_table();
}                                             
2
  • 1
    Learn about prepared statements Commented Jul 20, 2016 at 8:59
  • Ok. Thank you very much. Commented Jul 20, 2016 at 9:33

1 Answer 1

1

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);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.