开发学院WEB开发Jsp 在java应用程序中显示数据库的blob图像 阅读

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

 2008-01-05 10:53:48 来源:WEB开发网   
核心提示:原理:从数据库中读取blob到Blob对象. Blob blob = rs.getBlob(2);把blob传给Stream.用Stream参数创建Image对象显示Image对象.说明: 这只是例子, 没有考虑其他方面. 希望大家能对java连接数据库,和取对象有个具体的了解.关于数据库的sqlCREATE TABL

  原理:
  
  从数据库中读取blob到Blob对象. Blob blob = rs.getBlob(2);
  
  把blob传给Stream.
  
  用Stream参数创建Image对象
  
  显示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.*;
  
  /**
  
  *
  
  Title:
  *
  *
  
  Description:vincentlaw@163.com
  *
  *
  
  Copyright: Copyright (c) 2005
  *
  *
  
  Vincent
  *
  * @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
  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;
  
  /**
  
  *
  
  Title:
  *
  *
  
  Description:vincentlaw@163.com
  *
  *
  
  Copyright: Copyright (c) 2005
  *
  *
  
  Vincent
  *
  * @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);
  }
  
  }

Tags:java 应用程序 显示

编辑录入:爽爽 [复制链接] [打 印]
[]
  • 好
  • 好的评价 如果觉得好,就请您
      0%(0)
  • 差
  • 差的评价 如果觉得差,就请您
      0%(0)
赞助商链接