Sunday, July 28, 2013

A Swing Application to Store and Retrive an image from MYSQL


Hi , Today we are going to see how to store an image in MySQL and how to retrieve the image and display it to the user using Swing.

So First we need MySQL Data base if you don't have you can download it from oracle website.

So this is the table we are going to create:

Table : image

create table image
( id varchar(20),
image longblob);

I have created this table under the schema name "test" in MySQL ,So from the code i will be accessing  test schema for referring this table.

So we have two fields here one is id another one is image as type long blob which can store an image up to the size of 1 MB i guess.More then that will thrown an Exception.

Now ,we need to write our Java code to connect to this table and to store this image along with image file.

We also need Mysql-connector.jar .So download this jar and add it to the class path.

Code :

import java.awt.Toolkit;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FilenameFilter;
import java.sql.*;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileFilter;
public class StoreImage extends javax.swing.JFrame {
Connection connection=null;
PreparedStatement ps=null;
ResultSet rs=null;
String filePath=null;

public StoreImage() {

initComponents();
initConnection();
setSize(600,500);
}

public void retriveImage()
{
try
{
String val=jTextField1.getText();
if(val.length()>0)
{
ps=connection.prepareStatement("select * from image where id=?");
ps.setObject(1, val);
rs=ps.executeQuery();
byte b[] = null;
while(rs.next())
{
b= rs.getBytes(2);
}

jLabel6.setIcon(new ImageIcon (Toolkit.getDefaultToolkit().createImage(b)));


}else
{
JOptionPane.showMessageDialog(this,"Please enter ID..." );
}


}catch(Exception e)
{

}
}

//1048576 Size limit allowed for Image storage in MySQL.
public void storeImage()
{
try
{

JFileChooser chooser=new JFileChooser(new File("E:\\"));
 
chooser.setMultiSelectionEnabled(false);
chooser.setVisible(true);

chooser.showOpenDialog(this);

File file=chooser.getSelectedFile();
if(file!=null){filePath=file.getPath();}
if(filePath!=null){
jLabel5.setText("File:"+" "+filePath);
jLabel6.setIcon(new ImageIcon(filePath));
}


if(filePath!=null && check())
{
ps=connection.prepareStatement("insert into image values(?,?)");
FileInputStream fileInputStream=new FileInputStream(filePath);
byte b[]=new byte[fileInputStream.available()];
fileInputStream.read(b);
fileInputStream.close();
ps.setObject(1, jTextField1.getText());
ps.setBytes(2, b);
int val=ps.executeUpdate();
if(val>=1)JOptionPane.showMessageDialog(this, "Succesfully Stored...");
else
JOptionPane.showMessageDialog(this, "Error in storage...");

}
else
{
JOptionPane.showMessageDialog(this,"Please select an Image of type .jpeg/gif/jpg...");
}

}catch(Exception e)
{

JOptionPane.showMessageDialog(this, e.getMessage());
e.printStackTrace();
}
}

public void initConnection()
{
try
{

Class.forName("com.mysql.jdbc.Driver");
connection=DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","ayaz");
System.out.println("Connection Established Succcesfully...");
}
catch(Exception e)
{
JOptionPane.showMessageDialog(this, e.getMessage());
}
}

 
private void initComponents() {

jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jButton1 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
jTextField1 = new javax.swing.JTextField();
jLabel5 = new javax.swing.JLabel();
jScrollPane1 = new javax.swing.JScrollPane();
jLabel6 = new javax.swing.JLabel();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
getContentPane().setLayout(null);

jLabel1.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
jLabel1.setText("Store/Retive Image From MySQL");
getContentPane().add(jLabel1);
jLabel1.setBounds(90, 30, 220, 14);

jLabel2.setText("ID :");
getContentPane().add(jLabel2);
jLabel2.setBounds(60, 90, 18, 14);

jLabel3.setText("Select an Image :");
getContentPane().add(jLabel3);
jLabel3.setBounds(40, 130, 100, 14);

jButton1.setText("Browse");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
getContentPane().add(jButton1);
jButton1.setBounds(150, 123, 100, 30);

jButton3.setText("Show");
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton3ActionPerformed(evt);
}
});
getContentPane().add(jButton3);
jButton3.setBounds(130, 213, 80, 30);
getContentPane().add(jTextField1);
jTextField1.setBounds(150, 80, 90, 20);

jLabel5.setForeground(new java.awt.Color(255, 0, 0));
getContentPane().add(jLabel5);
jLabel5.setBounds(40, 170, 240, 30);

jScrollPane1.setViewportView(jLabel6);

getContentPane().add(jScrollPane1);
jScrollPane1.setBounds(330, 70, 210, 160);

pack();
  } 

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
  storeImage(); 
}

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
retriveImage();
}

public static void main(String args[]) {
   new StoreImage().setVisible(true);
}


// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton3;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextField jTextField1;
// End of variables declaration

private boolean check() {
if(filePath!=null)
{
if(filePath.endsWith(".jpeg")||filePath.endsWith(".gif")||filePath.endsWith(".jpg")||filePath.endsWith(".JPEG")||filePath.endsWith(".GIF")||filePath.endsWith(".JPG"))
{
return true;
}
return false;
}
return false;
}
}


Result :




















<< Prev                               Index                  Next>>

-------------------------------------------------------------------------------------







Technology Blogs
blogs

14 comments:

  1. I saw the above example, very good one. can i get above example workspace?

    ReplyDelete
  2. this is helpful i hava a problem if in swing gui i want to remove the button when i click the button like in games how it is possible in mouse click method i write this method {contentPane.remove(button)} but may its not too much

    ReplyDelete
  3. nice tutorial it was very useful to me

    ReplyDelete
  4. Listen My Friends
    There are Two Correction We need to do in the above sourcecode
    1. Remove initConnection() , instead this write drive creation and connection direct where we do database transaction.
    2. After Insertion done successfully put filepath as null otherwise it take same file again and again.

    ReplyDelete
  5. Good Post! , it was so good to read and useful to improve my knowledge as an updated one, keep blogging. After seeing your article I want to say that also a well-written article with some very good information which is very useful for the readers....thanks for sharing it and do share more posts like this.
    Data Science Online Training

    ReplyDelete

  6. Good Post! it was so good to read and useful to improve my knowledge as an updated one, keep blogging. After seeing your article, I want to say that also a well-written article with some very good information which is very useful for the AWS Online Training

    ReplyDelete
  7. Data analyst generally works on creation of reports based on company’s data driven KPI’s(generally involves descriptive analytics), whereas Data scientists understand business and domain along with the technicalities to understand what will happen in future(more on descriptive + predictive analytics both)
    Etlhive is a data science institute in pune. actuelly we wanted to promote our website on your site will you please contact me discus further details

    website: - www.etlhive.com
    contact: - +91 8055020011

    ReplyDelete