Display blob images of databases in java applications

Source: Internet
Author: User

Principle:
<1> Read blob to Blob Object from database. Blob blob = rs. getBlob (2 );
<2> pass blob to Stream.
<3> use the Stream parameter to create an Image object
<4> display the Image object.
Note: This is just an example. I do not consider other aspects. I hope you can have a detailed understanding of connecting to the database using java and getting objects.
SQL statements about databases
Create table photo (
Photo_no int (6) unsigned not null auto_increment,
Image blob,
Primary key ('photo _ no ')
)
In this example, two files are used:
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. getdefatooltoolkit (). getImage ("c: 1.jpg"); // default file
Try {
JbInit ();
} Catch (Exception ex ){
Ex. printStackTrace ();
}
}
Public void addpolicy (){
Super. addpolicy ();
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.exe cuteQuery ("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, local file loading ");
Im = Toolkit. getdefatooltoolkit (). 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 = 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_jtogglebutton?actionadapter (this ));
This. getContentPane (). add (jToggleButton1, java. awt. BorderLayout. NORTH );
This. getContentPane (). add (panel, java. awt. BorderLayout. CENTER );
}
Public void jtogglebutton?action=med (ActionEvent e ){
Panel. changeImage ("E: photohoto1281.jpg"); // backup image file
}

Public final static void main (String [] args ){
JFrame frame = new ShowImage ();
Frame. setSize (800,600 );
Frame. setdefaclocloseoperation (JFrame. EXIT_ON_CLOSE );
Frame. setVisible (true );
}
}

Class showimage_jtogglebutton?actionadapter implements ActionListener {
Private ShowImage adaptee;
Showimage_jtogglebutton?actionadapter (ShowImage adaptee ){
This. adaptee = adaptee;
}
Public void actionreceivmed (ActionEvent e ){
Adaptee. jtogglebutton?action=med (e );
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.