Advertisement
Guest User

Untitled

a guest
Nov 15th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.19 KB | None | 0 0
  1. package tes1;
  2.  
  3. //import com.mysql.jdbc.Connection;
  4. import java.awt.Image;
  5. import java.awt.image.BufferedImage;
  6. import java.io.ByteArrayInputStream;
  7. import java.io.IOException;
  8. import java.io.InputStream;
  9. import java.io.ObjectInputStream;
  10. import java.sql.Blob;
  11. import java.sql.Connection;
  12. import java.sql.DriverManager;
  13. import java.sql.ResultSet;
  14. import java.sql.SQLException;
  15. import java.sql.Statement;
  16. import java.util.logging.Level;
  17. import java.util.logging.Logger;
  18. import javax.imageio.ImageIO;
  19. import javax.swing.ImageIcon;
  20.  
  21. public class TampilGambarBlob extends javax.swing.JFrame {
  22.     ImageIcon newImage   = null;
  23.    
  24.     public TampilGambarBlob(){
  25.         tampilkan();
  26.         initComponents();
  27.     }
  28.  
  29.     void tampilkan()
  30.     {
  31.         try
  32.         {
  33.             Connection con = DriverManager.getConnection("jdbc:mysql://localhost/TesBlob","root","");
  34.             Statement stat = con.createStatement();
  35.             ResultSet rs = stat.executeQuery("Select foto from tes1 where no=2");
  36.             while(rs.next())
  37.             {
  38.                 //InputStream img = rs.getBinaryStream("foto");
  39.                 Blob imageBlob  = rs.getBlob("foto");
  40.                 byte[] img      = imageBlob.getBytes(1, (int)imageBlob.length());
  41.                 /*try
  42.                 {
  43.                     BufferedImage file = ImageIO.read(new ByteArrayInputStream(img));
  44.                 }
  45.                 catch(IOException e)
  46.                 {
  47.                    
  48.                 }*/
  49.                 BufferedImage file = ImageIO.read(new ByteArrayInputStream(img));
  50.                 ImageIcon image   = new ImageIcon(file);
  51.                 Image im      = image.getImage();
  52.                 Image myImg   = im.getScaledInstance(g1.getWidth(), g1.getHeight(),Image.SCALE_SMOOTH);
  53.                 ImageIcon newImage= new ImageIcon(myImg);
  54.                 //g1.setIcon(newImage);
  55.                
  56.             }
  57.         }
  58.         catch(SQLException e)
  59.         {
  60.             System.out.println("ERROR BOS ->" +e.getMessage());
  61.         } catch (IOException ex) {
  62.             Logger.getLogger(TampilGambarBlob.class.getName()).log(Level.SEVERE, null, ex);
  63.         } //catch (IOException ex) {
  64.                 }
  65.  
  66.     @SuppressWarnings("unchecked")
  67.     // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
  68.     private void initComponents() {
  69.  
  70.         jPanel1 = new javax.swing.JPanel();
  71.         g1 = new javax.swing.JLabel();
  72.  
  73.         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  74.  
  75.         jPanel1.setBackground(new java.awt.Color(255, 255, 255));
  76.  
  77.         g1.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
  78.         g1.setIcon(newImage);
  79.         g1.setText("GAMBAR");
  80.  
  81.         javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
  82.         jPanel1.setLayout(jPanel1Layout);
  83.         jPanel1Layout.setHorizontalGroup(
  84.             jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  85.             .addGroup(jPanel1Layout.createSequentialGroup()
  86.                 .addGap(162, 162, 162)
  87.                 .addComponent(g1)
  88.                 .addContainerGap(644, Short.MAX_VALUE))
  89.         );
  90.         jPanel1Layout.setVerticalGroup(
  91.             jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  92.             .addGroup(jPanel1Layout.createSequentialGroup()
  93.                 .addGap(58, 58, 58)
  94.                 .addComponent(g1)
  95.                 .addContainerGap(350, Short.MAX_VALUE))
  96.         );
  97.  
  98.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  99.         getContentPane().setLayout(layout);
  100.         layout.setHorizontalGroup(
  101.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  102.             .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  103.         );
  104.         layout.setVerticalGroup(
  105.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  106.             .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  107.         );
  108.  
  109.         pack();
  110.     }// </editor-fold>                        
  111.  
  112.     /**
  113.      * @param args the command line arguments
  114.      */
  115.     public static void main(String args[]) {
  116.         /* Set the Nimbus look and feel */
  117.         //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
  118.         /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
  119.          * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
  120.          */
  121.         try {
  122.             for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
  123.                 if ("Nimbus".equals(info.getName())) {
  124.                     javax.swing.UIManager.setLookAndFeel(info.getClassName());
  125.                     break;
  126.                 }
  127.             }
  128.         } catch (ClassNotFoundException ex) {
  129.             java.util.logging.Logger.getLogger(TampilGambarBlob.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  130.         } catch (InstantiationException ex) {
  131.             java.util.logging.Logger.getLogger(TampilGambarBlob.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  132.         } catch (IllegalAccessException ex) {
  133.             java.util.logging.Logger.getLogger(TampilGambarBlob.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  134.         } catch (javax.swing.UnsupportedLookAndFeelException ex) {
  135.             java.util.logging.Logger.getLogger(TampilGambarBlob.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  136.         }
  137.         //</editor-fold>
  138.  
  139.         /* Create and display the form */
  140.         java.awt.EventQueue.invokeLater(new Runnable() {
  141.             public void run() {
  142.                     new TampilGambarBlob().setVisible(true);
  143.                 }
  144.             });
  145.         }
  146.    
  147.  
  148.     // Variables declaration - do not modify                    
  149.     private javax.swing.JLabel g1;
  150.     private javax.swing.JPanel jPanel1;
  151.     // End of variables declaration                  
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement