user5789297
user5789297

Reputation: 11

Having trouble updating my items, image BLOB format [JAVA]

the program is a shop where you can add items with photos, you can update, search, delete,add etc, i can search, delete and add but can't edit my items. it kept saying that it has a wrong sql syntax, i don't know what to do. pls help.

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

has an error in my sql syntax, whats wrong with it?

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

Upvotes: 1

Views: 92

Answers (1)

Fabio J. Walter
Fabio J. Walter

Reputation: 31

I wrote this code to update images in a PostgreSQL Database.

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

Works fine!

Upvotes: 1

Related Questions