在java应用程序中显示数据库的blob图像

2008-02-23 09:32:30来源:互联网 阅读 ()

新老客户大回馈,云服务器低至5折

原理:

<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:vincentlaw@163.com </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:vincentlaw@163.com </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);
}

}

上一篇: JFreeChart 的用法
下一篇: java.io.ObjectInput翻译

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:简单的ant模版forJB2005

下一篇:To invoke Web Service in .htm file