【问题标题】:How to update file path with \\ in mysql using java?如何使用java在mysql中使用\\更新文件路径?
【发布时间】:2016-11-23 10:09:52
【问题描述】:

用新文件更新附件字段后,mysql文件路径没有斜杠。谁能告诉我修复它的路径?

这是我的文件附件按钮:

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); 
}

这是我的更新按钮代码:

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();
}                                             

【问题讨论】:

  • 了解准备好的语句
  • 好的。非常感谢。

标签: java mysql jdbc netbeans


【解决方案1】:

使用 PreparedStatement? 不像原生查询,这是一个如何正确使用它的示例:

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);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-16
    • 2019-06-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多