该程序是一个商店,您可以在其中添加带照片的项目,您可以更新,搜索,删除,添加等,我可以搜索,删除和添加但不能编辑我的项目。它一直说它有一个错误的SQL语法,我不知道该怎么做。请帮助。
private void btn_browseActionPerformed(java.awt.event.ActionEvent evt) {
fileChooser.setCurrentDirectory(new File(System.getProperty("user.home")));
FileNameExtensionFilter filter = new FileNameExtensionFilter("*.IMAGE", "jpg", "gif", "png");
fileChooser.addChoosableFileFilter(filter);
result = fileChooser.showSaveDialog(null);
if (result == JFileChooser.APPROVE_OPTION) {
File selectedFile = fileChooser.getSelectedFile();
String path = selectedFile.getAbsolutePath();
label_photo.setIcon(ResizeImage(path));
s = path;
tf = "true";
} else if (result == JFileChooser.CANCEL_OPTION) {
System.out.println("No Data");
tf = "false";
} else {
tf = "false";
}
}
我的sql语法有错误,它有什么问题吗?
public void update() {
try {
Class.forName("com.mysql.jdbc.Driver"); // MySQL database connection
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/cake_ordering_system?" + "user=root&password=");
PreparedStatement pstmt = null;
pstmt = conn.prepareStatement("update cake set cake_name=?,cake_description=?,cake_price=?,cake_photo=? where cake_name='" + tf_search + "'");
InputStream is = new FileInputStream(new File(s));
//cake_name
pstmt.setString(1, tf_name.getText());
//cake_description*/
pstmt.setString(2, ta_dc.getText());
//cake_price
pstmt.setString(3, tf_price.getText());
//cake_photo
pstmt.setBinaryStream(4, is);
//execute the query
pstmt.executeUpdate();
JOptionPane.showMessageDialog(null, "Successfully updated a new record!");
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e);
}
}
答案 0 :(得分:1)
我编写了这段代码来更新PostgreSQL数据库中的图像。
private void updateImage(int id, File tempImagem) throws Exception {
try {
FileInputStream fis = new FileInputStream(tempImagem);
PreparedStatement pstm = super.operaConn.getConnDst().getConexao().prepareStatement("UPDATE table SET image = ? WHERE id = ?");
byte[] imagemArray = new byte[(int) tempImagem.length()];
DataInputStream imagemStream = new DataInputStream(new FileInputStream(tempImagem));
imagemStream.readFully(imagemArray);
imagemStream.close();
pstm.setBytes(1, imagemArray);
pstm.setInt(2, id);
pstm.executeUpdate();
pstm.close();
fis.close();
} catch (Exception e) {
throw new Exception("ERRO no metodo updateImagem()", e);
}
}
工作正常!