Jumat, 10 April 2015

JAVA - How To Retrieve Image From MySQL Database Using Java

JAVA - How To Retrieve Image From MySQL Database In Java

__________________________________________________________________________

In this java Tutorial we will see How To Get Picture from MySQL database
In Java NetBeans .


*Java Courses Java Complete Course

                            Java Swing Course


Project Source Code:




package JavaDB_001; import java.awt.Color; import java.awt.Image; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; import javax.swing.*;  public class Work extends JFrame{     JButton button ;     JLabel label;     JTextField jtf;      public Work(){     super("retrieve image from database in java");          button = new JButton("Retrieve");     button.setBounds(250,300,100,40);          jtf = new JTextField();     jtf.setBounds(360,310,100,20);          label = new JLabel();     label.setBounds(10,10,670,250);          add(button);     add(label);     add(jtf);          button.addActionListener(new ActionListener() {          @Override         public void actionPerformed(ActionEvent e) {                      try{                 Connection con = DriverManager.getConnection("jdbc:mysql://localhost/db_images","root","");                 Statement st = con.createStatement();                 ResultSet rs = st.executeQuery("select * from myimages where ID = '"+jtf.getText()+"'");                 if(rs.next()){                     byte[] img = rs.getBytes("Image");                        //Resize The ImageIcon                     ImageIcon image = new ImageIcon(img);                     Image im = image.getImage();                     Image myImg = im.getScaledInstance(label.getWidth(), label.getHeight(),Image.SCALE_SMOOTH);                     ImageIcon newImage = new ImageIcon(myImg);                     label.setIcon(newImage);                 }                                  else{                     JOptionPane.showMessageDialog(null, "No Data");                 }             }catch(Exception ex){                 ex.printStackTrace();             }                  }     });          setLayout(null);     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);     getContentPane().setBackground(Color.decode("#bdb76b"));     setLocationRelativeTo(null);     setSize(700,400);     setVisible(true);     }            public static void main(String[] args){         new Work();     }    } 
/////////////////////  END
///////////////OUTPUT:

showing image from mysql database


Bagikan

Jangan lewatkan

JAVA - How To Retrieve Image From MySQL Database Using Java
4/ 5
Oleh

Subscribe via email

Suka dengan artikel di atas? Tambahkan email Anda untuk berlangganan.