【问题标题】:Retrieve and update java derby database blog item检索和更新 java derby 数据库博客项目
【发布时间】:2014-01-23 15:23:14
【问题描述】:

我有一个编辑例程,允许我为特定数据库创建、编辑和保存记录。在这个例程中,我有一个 jlable,用于显示图像(imageIcon),在初始创建或记录编辑每个记录时从磁盘文件加载该图像。毫无问题地工作。

我需要获取该 imageIcon 而不是磁盘文件(以及该特定记录的所有关联数据)并将其存储在嵌入式数据库中(图像的 blob 类型)。除图像存储外,数据库已创建、初始化并正常工作。

图像存储后,当每条记录被访问时,jlable会显示存储在数据库中的图像。原始磁盘文件(JPG、PNG 等)将无法加载。在初始创建记录后,唯一会使用磁盘文件的情况是用户希望将图像更改为不同的图像。

简单地 - 两个例程: 1. 取一个 imageIcon,将其保存到数据库 blob。 2. 检索数据库 blob 并将其显示到 imageIcon。

【问题讨论】:

  • 你试过什么?它奏效了吗?你遇到什么问题?您只是在寻找 BLOB 示例代码吗?您查看过 JDBC 教程或 Javadocs 吗?
  • 根本不知道该怎么做……除非它像普通的数据库操作一样,但我怀疑不是……示例确实有助于理解如何做到这一点…… ..

标签: java database blob derby


【解决方案1】:

很抱歉回答您的问题为时已晚,因为我刚刚开始使用 Derby(我在 Derby 之前使用过 MySQL)。此代码从 JButton 图标中保存 BLOB,该图标取自 JFileChooser 对话框。代码是键域,表的主键和 field 是表示 BLOB 字段名称的字符串。图片是从路径目录中选择的:

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

这是 ImagePreview 组件类:

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

您可以尝试使用此站点检索 BLOB:http://www.jguru.com/faq/view.jsp?EID=1325#

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-21
    • 2015-05-06
    相关资源
    最近更新 更多