gpt4 book ai didi

java - 如何使用 java 在 mysql 中更新文件路径?

转载 作者:行者123 更新时间:2023-11-28 23:28:06 25 4
gpt4 key购买 nike

用新文件更新附件字段后,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();
}

最佳答案

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

关于java - 如何使用 java 在 mysql 中更新文件路径?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38476742/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com