Program | data | database | display
Principle:
<1> reads a blob to a BLOB object from the database. Blob blob = Rs.getblob (2);
<2> Pass the blob to the stream.
<3> Create an Image object with the stream parameter
<4> Displays the image object.
Description: This is just an example and does not consider other aspects. I hope you can connect to the Java database, and the object has a specific understanding.
About the SQL of the database
CREATE TABLE Photo (
Photo_no Int (6) unsigned not NULL auto_increment,
Image blob,
PRIMARY KEY (' Photo_no ')
)
This example uses two files:
Drawpanel.java is as follows
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"); The default file
try {
Jbinit ();
catch (Exception ex) {
Ex.printstacktrace ();
}
}
public void Addnotify () {
Super.addnotify ();
insets = Getinsets ();
SetBounds (MB, 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);
Database connection
InputStream fin = Blob.getbinarystream ();
im = Javax.imageio.ImageIO.read (FIN);
Rs.close ();
Stmt.close ();
Conn.close ();
}
catch (Exception e)
{
E.printstacktrace ();
Joptionpane.showmessagedialog (NULL, "error, load local file");
Im=toolkit.getdefaulttoolkit (). GetImage (FileName);
}
This.repaint ();
}
}
Showimage.java is as follows:
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"); Alternate image file
}
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);
}
}