In this video i show you how display image from mysql database using java application.last video I show you how insert image to mysql database.You can download all source code
related this project.
related this project.
Source code
package image;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import java.awt.Image;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
/**
*
* @author ccs
*/
public class Main extends javax.swing.JFrame {
/**
* Creates new form Main
*/
private ImageIcon format=null;
String fname=null;
int s=0;
byte[] pimage=null;
Connection conn=null;
public Main() {
initComponents();
conn=DBConnect.connect();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
jTabbedPane1 = new javax.swing.JTabbedPane();
jPanel2 = new javax.swing.JPanel();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
txtdisid = new javax.swing.JTextField();
lbldisplay = new javax.swing.JLabel();
Insert = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
txtid = new javax.swing.JTextField();
lblimage = new javax.swing.JLabel();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jPanel2.setBackground(new java.awt.Color(0, 255, 204));
jLabel3.setFont(new java.awt.Font("Sitka Text", 3, 36)); // NOI18N
jLabel3.setForeground(new java.awt.Color(255, 0, 51));
jLabel3.setText("Image Display");
jLabel4.setFont(new java.awt.Font("Sitka Text", 3, 14)); // NOI18N
jLabel4.setText("Image ID");
txtdisid.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyReleased(java.awt.event.KeyEvent evt) {
txtdisidKeyReleased(evt);
}
});
lbldisplay.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0), 2));
javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
jPanel2.setLayout(jPanel2Layout);
jPanel2Layout.setHorizontalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addGap(26, 26, 26)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 89, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(txtdisid, javax.swing.GroupLayout.PREFERRED_SIZE, 155, javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 263, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(41, 41, 41)
.addComponent(lbldisplay, javax.swing.GroupLayout.PREFERRED_SIZE, 282, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 0, Short.MAX_VALUE))
);
jPanel2Layout.setVerticalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(63, 63, 63)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(txtdisid, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGroup(jPanel2Layout.createSequentialGroup()
.addGap(21, 21, 21)
.addComponent(lbldisplay, javax.swing.GroupLayout.PREFERRED_SIZE, 281, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap(22, Short.MAX_VALUE))
);
jTabbedPane1.addTab("Display Image", jPanel2);
Insert.setBackground(new java.awt.Color(255, 255, 102));
jLabel1.setFont(new java.awt.Font("Tahoma", 3, 36)); // NOI18N
jLabel1.setForeground(new java.awt.Color(255, 0, 102));
jLabel1.setText("Image Insert");
jLabel2.setFont(new java.awt.Font("Tahoma", 3, 14)); // NOI18N
jLabel2.setText("Image ID");
lblimage.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0), 2));
jButton1.setBackground(new java.awt.Color(0, 255, 51));
jButton1.setFont(new java.awt.Font("Sitka Text", 3, 18)); // NOI18N
jButton1.setText("Choose Image");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jButton2.setBackground(new java.awt.Color(0, 255, 51));
jButton2.setFont(new java.awt.Font("Sitka Text", 3, 18)); // NOI18N
jButton2.setText("Insert Image");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
javax.swing.GroupLayout InsertLayout = new javax.swing.GroupLayout(Insert);
Insert.setLayout(InsertLayout);
InsertLayout.setHorizontalGroup(
InsertLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(InsertLayout.createSequentialGroup()
.addGroup(InsertLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(InsertLayout.createSequentialGroup()
.addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(txtid, javax.swing.GroupLayout.PREFERRED_SIZE, 149, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(InsertLayout.createSequentialGroup()
.addContainerGap()
.addGroup(InsertLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 251, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 251, javax.swing.GroupLayout.PREFERRED_SIZE))))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 96, Short.MAX_VALUE)
.addGroup(InsertLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(lblimage, javax.swing.GroupLayout.PREFERRED_SIZE, 245, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 245, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap())
);
InsertLayout.setVerticalGroup(
InsertLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(InsertLayout.createSequentialGroup()
.addGroup(InsertLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(InsertLayout.createSequentialGroup()
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addGroup(InsertLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(txtid, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGroup(InsertLayout.createSequentialGroup()
.addContainerGap()
.addComponent(lblimage, javax.swing.GroupLayout.PREFERRED_SIZE, 237, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(InsertLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(29, Short.MAX_VALUE))
);
jTabbedPane1.addTab("Insert Image", Insert);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jTabbedPane1)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 352, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 0, Short.MAX_VALUE))
);
pack();
}// </editor-fold>//GEN-END:initComponents
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
JFileChooser fchoser=new JFileChooser();
fchoser.showOpenDialog(null);
File f=fchoser.getSelectedFile();
fname=f.getAbsolutePath();
ImageIcon micon=new ImageIcon(fname);
try {
File image=new File(fname);
FileInputStream fis=new FileInputStream(image);
ByteArrayOutputStream baos=new ByteArrayOutputStream();
byte[] buf=new byte[1024];
for(int readnum; (readnum=fis.read(buf)) !=-1;)
{
baos.write(buf,0,readnum);
}
pimage=baos.toByteArray();
lblimage.setIcon(resizeImage(fname, buf));
} catch (Exception e) {
}
}//GEN-LAST:event_jButton1ActionPerformed
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
String id=txtid.getText();
try {
String q= "INSERT INTO `img`(`id`, `image`) VALUES (?,?)";
PreparedStatement pst=conn.prepareStatement(q);
pst.setString(1, id);
pst.setBytes(2, pimage);
pst.execute();
} catch (Exception e) {
}
}//GEN-LAST:event_jButton2ActionPerformed
private void txtdisidKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtdisidKeyReleased
try {
String sql="SELECT `image` FROM `img` WHERE id='"+txtdisid.getText()+"'";
PreparedStatement pst=conn.prepareStatement(sql);
ResultSet rs=pst.executeQuery();
if(rs.next())
{
byte[] imagedata=rs.getBytes("image");
format=new ImageIcon(imagedata);
Image mm=format.getImage();
Image img2=mm.getScaledInstance(lbldisplay.getWidth(), lbldisplay.getHeight(), Image.SCALE_SMOOTH);
ImageIcon image=new ImageIcon(img2);
lbldisplay.setIcon(image);
}
else
{
}
} catch (Exception e) {
}
}//GEN-LAST:event_txtdisidKeyReleased
//private ImageIcon format=null;
public ImageIcon resizeImage(String imagePath, byte[] pic){
ImageIcon myImage=null;
if(imagePath !=null)
{
myImage=new ImageIcon(imagePath);
}else{
myImage=new ImageIcon(pic);
}
Image img=myImage.getImage();
Image img2=img.getScaledInstance(lblimage.getHeight(), lblimage.getWidth(), Image.SCALE_SMOOTH);
ImageIcon image=new ImageIcon(img2);
return image;
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Main().setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JPanel Insert;
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JPanel jPanel2;
private javax.swing.JTabbedPane jTabbedPane1;
private javax.swing.JLabel lbldisplay;
private javax.swing.JLabel lblimage;
private javax.swing.JTextField txtdisid;
private javax.swing.JTextField txtid;
// End of variables declaration//GEN-END:variables
}
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import java.awt.Image;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
/**
*
* @author ccs
*/
public class Main extends javax.swing.JFrame {
/**
* Creates new form Main
*/
private ImageIcon format=null;
String fname=null;
int s=0;
byte[] pimage=null;
Connection conn=null;
public Main() {
initComponents();
conn=DBConnect.connect();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
jTabbedPane1 = new javax.swing.JTabbedPane();
jPanel2 = new javax.swing.JPanel();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
txtdisid = new javax.swing.JTextField();
lbldisplay = new javax.swing.JLabel();
Insert = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
txtid = new javax.swing.JTextField();
lblimage = new javax.swing.JLabel();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jPanel2.setBackground(new java.awt.Color(0, 255, 204));
jLabel3.setFont(new java.awt.Font("Sitka Text", 3, 36)); // NOI18N
jLabel3.setForeground(new java.awt.Color(255, 0, 51));
jLabel3.setText("Image Display");
jLabel4.setFont(new java.awt.Font("Sitka Text", 3, 14)); // NOI18N
jLabel4.setText("Image ID");
txtdisid.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyReleased(java.awt.event.KeyEvent evt) {
txtdisidKeyReleased(evt);
}
});
lbldisplay.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0), 2));
javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
jPanel2.setLayout(jPanel2Layout);
jPanel2Layout.setHorizontalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addGap(26, 26, 26)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 89, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(txtdisid, javax.swing.GroupLayout.PREFERRED_SIZE, 155, javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 263, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(41, 41, 41)
.addComponent(lbldisplay, javax.swing.GroupLayout.PREFERRED_SIZE, 282, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 0, Short.MAX_VALUE))
);
jPanel2Layout.setVerticalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(63, 63, 63)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(txtdisid, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGroup(jPanel2Layout.createSequentialGroup()
.addGap(21, 21, 21)
.addComponent(lbldisplay, javax.swing.GroupLayout.PREFERRED_SIZE, 281, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap(22, Short.MAX_VALUE))
);
jTabbedPane1.addTab("Display Image", jPanel2);
Insert.setBackground(new java.awt.Color(255, 255, 102));
jLabel1.setFont(new java.awt.Font("Tahoma", 3, 36)); // NOI18N
jLabel1.setForeground(new java.awt.Color(255, 0, 102));
jLabel1.setText("Image Insert");
jLabel2.setFont(new java.awt.Font("Tahoma", 3, 14)); // NOI18N
jLabel2.setText("Image ID");
lblimage.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0), 2));
jButton1.setBackground(new java.awt.Color(0, 255, 51));
jButton1.setFont(new java.awt.Font("Sitka Text", 3, 18)); // NOI18N
jButton1.setText("Choose Image");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jButton2.setBackground(new java.awt.Color(0, 255, 51));
jButton2.setFont(new java.awt.Font("Sitka Text", 3, 18)); // NOI18N
jButton2.setText("Insert Image");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
javax.swing.GroupLayout InsertLayout = new javax.swing.GroupLayout(Insert);
Insert.setLayout(InsertLayout);
InsertLayout.setHorizontalGroup(
InsertLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(InsertLayout.createSequentialGroup()
.addGroup(InsertLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(InsertLayout.createSequentialGroup()
.addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(txtid, javax.swing.GroupLayout.PREFERRED_SIZE, 149, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(InsertLayout.createSequentialGroup()
.addContainerGap()
.addGroup(InsertLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 251, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 251, javax.swing.GroupLayout.PREFERRED_SIZE))))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 96, Short.MAX_VALUE)
.addGroup(InsertLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(lblimage, javax.swing.GroupLayout.PREFERRED_SIZE, 245, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 245, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap())
);
InsertLayout.setVerticalGroup(
InsertLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(InsertLayout.createSequentialGroup()
.addGroup(InsertLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(InsertLayout.createSequentialGroup()
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addGroup(InsertLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(txtid, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGroup(InsertLayout.createSequentialGroup()
.addContainerGap()
.addComponent(lblimage, javax.swing.GroupLayout.PREFERRED_SIZE, 237, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(InsertLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(29, Short.MAX_VALUE))
);
jTabbedPane1.addTab("Insert Image", Insert);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jTabbedPane1)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 352, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 0, Short.MAX_VALUE))
);
pack();
}// </editor-fold>//GEN-END:initComponents
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
JFileChooser fchoser=new JFileChooser();
fchoser.showOpenDialog(null);
File f=fchoser.getSelectedFile();
fname=f.getAbsolutePath();
ImageIcon micon=new ImageIcon(fname);
try {
File image=new File(fname);
FileInputStream fis=new FileInputStream(image);
ByteArrayOutputStream baos=new ByteArrayOutputStream();
byte[] buf=new byte[1024];
for(int readnum; (readnum=fis.read(buf)) !=-1;)
{
baos.write(buf,0,readnum);
}
pimage=baos.toByteArray();
lblimage.setIcon(resizeImage(fname, buf));
} catch (Exception e) {
}
}//GEN-LAST:event_jButton1ActionPerformed
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
String id=txtid.getText();
try {
String q= "INSERT INTO `img`(`id`, `image`) VALUES (?,?)";
PreparedStatement pst=conn.prepareStatement(q);
pst.setString(1, id);
pst.setBytes(2, pimage);
pst.execute();
} catch (Exception e) {
}
}//GEN-LAST:event_jButton2ActionPerformed
private void txtdisidKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtdisidKeyReleased
try {
String sql="SELECT `image` FROM `img` WHERE id='"+txtdisid.getText()+"'";
PreparedStatement pst=conn.prepareStatement(sql);
ResultSet rs=pst.executeQuery();
if(rs.next())
{
byte[] imagedata=rs.getBytes("image");
format=new ImageIcon(imagedata);
Image mm=format.getImage();
Image img2=mm.getScaledInstance(lbldisplay.getWidth(), lbldisplay.getHeight(), Image.SCALE_SMOOTH);
ImageIcon image=new ImageIcon(img2);
lbldisplay.setIcon(image);
}
else
{
}
} catch (Exception e) {
}
}//GEN-LAST:event_txtdisidKeyReleased
//private ImageIcon format=null;
public ImageIcon resizeImage(String imagePath, byte[] pic){
ImageIcon myImage=null;
if(imagePath !=null)
{
myImage=new ImageIcon(imagePath);
}else{
myImage=new ImageIcon(pic);
}
Image img=myImage.getImage();
Image img2=img.getScaledInstance(lblimage.getHeight(), lblimage.getWidth(), Image.SCALE_SMOOTH);
ImageIcon image=new ImageIcon(img2);
return image;
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Main().setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JPanel Insert;
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JPanel jPanel2;
private javax.swing.JTabbedPane jTabbedPane1;
private javax.swing.JLabel lbldisplay;
private javax.swing.JLabel lblimage;
private javax.swing.JTextField txtdisid;
private javax.swing.JTextField txtid;
// End of variables declaration//GEN-END:variables
}
Database Connection
package image;
import java.sql.Connection;
import java.sql.DriverManager;
/**
*
* @author ccs
*/
public class DBConnect {
public static Connection connect()
{
Connection con=null;
try {
Class.forName("com.mysql.jdbc.Driver");
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/image?","root","");
// JOptionPane.showConfirmDialog(null,"connect to data base");
} catch (Exception e) {
System.out.println("inter.DBConnect.connect()");
// JOptionPane.showConfirmDialog(null,e);
}
return con;
}
}
import java.sql.Connection;
import java.sql.DriverManager;
/**
*
* @author ccs
*/
public class DBConnect {
public static Connection connect()
{
Connection con=null;
try {
Class.forName("com.mysql.jdbc.Driver");
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/image?","root","");
// JOptionPane.showConfirmDialog(null,"connect to data base");
} catch (Exception e) {
System.out.println("inter.DBConnect.connect()");
// JOptionPane.showConfirmDialog(null,e);
}
return con;
}
}
No comments:
Post a Comment