原理:
<1> 从数据库中读取blob到Blob对象. Blob blob = rs.getBlob(2);
<2>把blob传给Stream.
<3>用Stream参数创建Image对象
<4>显示Image对象.
说明: 这只是例子, 没有考虑其他方面. 希望大家能对java连接数据库,和取对象有个具体的了解.
关于数据库的sql
CREATE TABLE photo (
photo_no int(6) unsigned NOT NULL auto_increment,
image blob,
PRIMARY KEY (`photo_no`)
)
本例子用到两个文件:
DrawPanel.java如下
import java.awt.*;
import java.sql.*;
import java.awt.image.*;
import java.io.*;
import javax.swing.*;
/**
* <p>Title: </p>
*
* <p>Description:[email protected] </p>
*
* <p>Copyright: Copyright (c) 2005</p>
*
* <p>Vincent</p>
*
* @author vincent
* @version 1.0
*/
public class DrawPanel extends Panel {
Image im;
Insets insets;
public DrawPanel() {
im=Toolkit.getDefaultToolkit().getImage("c:1.jpg"); //默认的文件
try {
jbInit();
} catch (Exception ex) {
ex.printStackTrace();
}
}
public void addNotify() {
super.addNotify();
insets = getInsets();
setBounds(100, 100, 217 + insets.left, 321 + insets.top);
}
public void paint(Graphics g) {
g.drawImage(im, insets.left, insets.top, this);
}
private void jbInit() throws Exception {
}
public void changeImage(String fileName)
{
try {
//Class.forName("oracle.jdbc.driver.OracleDriver");
//Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:orcl2", "user","password");
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/job?user=root&password=5672831");
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select photo_no,photo_image from photo");
rs.next();
Blob blob = rs.getBlob(2);
// 数据库连接
InputStream fin = blob.getBinaryStream();
im = javax.imageio.ImageIO.read(fin);
rs.close();
stmt.close();
conn.close();
}
catch(Exception e)
{
e.printStackTrace();
JOptionPane.showMessageDialog(null,"错误,装载本地文件");
im=Toolkit.getDefaultToolkit().getImage(fileName);
}
this.repaint();
}
}
ShowImage.java如下:
import java.awt.BorderLayout;
import java.awt.*;
import javax.swing.*;
import javax.swing.JToggleButton;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
* <p>Title: </p>
*
* <p>Description:[email protected] </p>
*
* <p>Copyright: Copyright (c) 2005</p>
*
* <p>Vincent</p>
*
* @author vincent
* @version 1.0
*/
public class ShowImage extends JFrame {
BorderLayout borderLayout1 = new BorderLayout();
JToggleButton jToggleButton1 = new JToggleButton();
DrawPanel panel=new DrawPanel();
public ShowImage() {
try {
jbInit();
} catch (Exception exception) {
exception.printStackTrace();
}
}
private void jbInit() throws Exception {
getContentPane().setLayout(borderLayout1);
jToggleButton1.setText("jToggleButton1");
jToggleButton1.addActionListener(new
ShowImage_jToggleButton1_actionAdapter(this));
this.getContentPane().add(jToggleButton1, java.awt.BorderLayout.NORTH);
this.getContentPane().add(panel,java.awt.BorderLayout.CENTER);
}
public void jToggleButton1_actionPerformed(ActionEvent e) {
panel.changeImage("E:photohoto1281.jpg"); //备用图像文件
}
public final static void main(String[] args){
JFrame frame=new ShowImage();
frame.setSize(800,600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
class ShowImage_jToggleButton1_actionAdapter implements ActionListener {
private ShowImage adaptee;
ShowImage_jToggleButton1_actionAdapter(ShowImage adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jToggleButton1_actionPerformed(e);
}
}