简体   繁体   中英

Retrieve and update java derby database blog item

I have a editing routine that will allow me to create, edit, and save records for a particular database. In this routine, I have a jlable that I use to display an image (imageIcon) that upon initial creation or record editing of each record is loaded from a disk file. Works with no probs.

I need to take that imageIcon not the disk file, (along with all associated data for that particular record) and store it in an embedded database (blob type for the image). Database is already created, initialized, and working except for the image storage.

After the image is stored, when each record is accessed, the jlable will display the image stored in the database. The original disk file (JPG, PNG, ect) will NOT be available to load. After initial creation of the record(s), the only time that a disk file would be used will be if the user wishes to change the image to a different one.

simply - two routines: 1. take an imageIcon, save it to a database blob. 2. Retrieve database blob and display it to the imageIcon.

I'm sorry for too late answering your question, because I'm just starting to use Derby(I used MySQL before Derby). This code saving BLOB from JButton icon that is taken from JFileChooser dialog. code is keyfield, primary key of table and field is string representing the name of BLOB field. Picture is selected from Path directory :

 public void SearchAndSave(JButton tombol) throws FileNotFoundException, 
 SQLException, IOException {

    //  set default directory from JFileChooser 

    JFileChooser fc = new JFileChooser(Path);
    fc.setAccessory(new ImagePreviewComponent(fc));      
    int returnVal = fc.showOpenDialog(null);
    if (returnVal == JFileChooser.APPROVE_OPTION) {
        File file = fc.getSelectedFile();
        String dir = file.getAbsolutePath();
        ImageIcon icon = new ImageIcon(dir);
        String fname = file.getName();                                                       
        tombol.setText("");  // tombol is JButton                 
        tombol.setIcon(icon);                                
        fileinputstream = new FileInputStream(file);
        String simpan = JOptionPane.showInputDialog("Save image ? ((y/n))");
        if (simpan.equals("y")) {
            PreparedStatement ps = null;
            Connection con = Database.getConnection();
            String SQL = "INSERT INTO " + namatabel; // name of table

            SQL = SQL + " (" + keyfield + "," +field + ") ";                        
            SQL = SQL + "VALUES (?,?)";
            ps = con.prepareStatement(SQL);

            try {               
                ps.setString(1, code);
                Blob blob = con.createBlob();                  
                ObjectOutputStream oos;
                oos = new ObjectOutputStream(blob.setBinaryStream(1));
                oos.writeObject(icon);
                oos.close();
                ps.setBlob(2, blob);                      
                if (ps.executeUpdate() > 0)                       
                    JOptionPane.showMessageDialog(null, "Image is saved");                                       
                else
                    JOptionPane.showMessageDialog(null, "Not saved");     
                blob.free();
                ps.close();
            } 
            catch(SQLException e) {   
                if (e.getErrorCode() == 1406) {
                    String msg = "Image size is too big";                  
                    JOptionPane.showMessageDialog(null, msg);
                }
                e.printStackTrace();                
            }
        }                         
    }        
 }

And this is ImagePreviewcomponent class :

 package util;
 import java.awt.Dimension;
 import java.awt.Graphics;
 import java.awt.Image;
 import java.beans.PropertyChangeEvent;
 import java.beans.PropertyChangeListener;
 import java.io.File;
 import javax.swing.ImageIcon;
 import javax.swing.JComponent;
 import javax.swing.JFileChooser;
 public class ImagePreviewComponent extends JComponent implements 
 PropertyChangeListener {
 ImageIcon thumbnailImage = null;
 File file = null;
 Double widthBox = 100.0;
 Double heightBox = 100.0;

public ImagePreviewComponent(JFileChooser fc) {
    setPreferredSize(new Dimension(widthBox.intValue(), heightBox.intValue()));
    fc.addPropertyChangeListener(this);
}

public double getBestScale(ImageIcon imageIcon) {
    double bestScale;
    if ((widthBox >= imageIcon.getIconWidth()) && (heightBox >= imageIcon.getIconHeight())) {
        bestScale = 1;
    } else {
        double widthScale = widthBox / imageIcon.getIconWidth();
        double heightScale = heightBox / imageIcon.getIconHeight();

        if (widthScale > heightScale) {
            bestScale = heightScale;
        } else {
            bestScale = widthScale;
        }
    }
    return bestScale;
}

public void propertyChange(PropertyChangeEvent e) {
    String prop = e.getPropertyName();
    if (JFileChooser.DIRECTORY_CHANGED_PROPERTY.equals(prop)) {
        file = null;
    } else if (JFileChooser.SELECTED_FILE_CHANGED_PROPERTY.equals(prop)) {
        file = (File) e.getNewValue();
    }

    if (file == null) {
        thumbnailImage = null;
    } else {
        ImageIcon imageIcon = new ImageIcon(file.getPath());
        double bestScale = getBestScale(imageIcon);
        thumbnailImage = new ImageIcon(imageIcon.getImage().getScaledInstance(
                (int) ((double) imageIcon.getIconWidth() * bestScale),
                (int) ((double) imageIcon.getIconHeight() * bestScale),
                Image.SCALE_DEFAULT));
    }
    repaint();
}

@Override
protected void paintComponent(Graphics g) {
    if (thumbnailImage != null) {
        int x = getWidth() / 2 - thumbnailImage.getIconWidth() / 2;
        int y = getHeight() / 2 - thumbnailImage.getIconHeight() / 2;
        thumbnailImage.paintIcon(this, g, x, y);
    }
}
}

You can try this site for retrieving BLOB : http://www.jguru.com/faq/view.jsp?EID=1325# .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM