我有一个编辑例程,允许我创建,编辑和保存特定数据库的记录。在这个例程中,我有一个jlable用于显示一个图像(imageIcon),在初始创建或记录编辑每个记录时从磁盘文件加载。没有probs。
我需要将imageIcon不是磁盘文件(以及该特定记录的所有相关数据)并将其存储在嵌入式数据库(图像的blob类型)中。除了图像存储之外,数据库已经创建,初始化和工作。
存储图像后,当访问每个记录时,jlable将显示存储在数据库中的图像。原始磁盘文件(JPG,PNG,ect)将无法加载。在初始创建记录之后,只有当用户希望将图像更改为其他图像时,才会使用磁盘文件。
简单地说 - 两个例程: 1.获取imageIcon,将其保存到数据库blob中。 2.检索数据库blob并将其显示到imageIcon。答案 0 :(得分:0)
我很抱歉回答你的问题为时已晚,因为我刚刚开始使用Derby(我在Derby之前使用过MySQL)。此代码从JButton图标中保存BLOB,该图标取自JFileChooser对话框。代码是keyfield,表的主键和 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();
}
}
}
}
这是ImagePreviewcomponent类:
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#。