Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package tes1;
- //import com.mysql.jdbc.Connection;
- import java.awt.Image;
- import java.awt.image.BufferedImage;
- import java.io.ByteArrayInputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.ObjectInputStream;
- import java.sql.Blob;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- import javax.imageio.ImageIO;
- import javax.swing.ImageIcon;
- public class TampilGambarBlob extends javax.swing.JFrame {
- ImageIcon newImage = null;
- public TampilGambarBlob(){
- tampilkan();
- initComponents();
- }
- void tampilkan()
- {
- try
- {
- Connection con = DriverManager.getConnection("jdbc:mysql://localhost/TesBlob","root","");
- Statement stat = con.createStatement();
- ResultSet rs = stat.executeQuery("Select foto from tes1 where no=2");
- while(rs.next())
- {
- //InputStream img = rs.getBinaryStream("foto");
- Blob imageBlob = rs.getBlob("foto");
- byte[] img = imageBlob.getBytes(1, (int)imageBlob.length());
- /*try
- {
- BufferedImage file = ImageIO.read(new ByteArrayInputStream(img));
- }
- catch(IOException e)
- {
- }*/
- BufferedImage file = ImageIO.read(new ByteArrayInputStream(img));
- ImageIcon image = new ImageIcon(file);
- Image im = image.getImage();
- Image myImg = im.getScaledInstance(g1.getWidth(), g1.getHeight(),Image.SCALE_SMOOTH);
- ImageIcon newImage= new ImageIcon(myImg);
- //g1.setIcon(newImage);
- }
- }
- catch(SQLException e)
- {
- System.out.println("ERROR BOS ->" +e.getMessage());
- } catch (IOException ex) {
- Logger.getLogger(TampilGambarBlob.class.getName()).log(Level.SEVERE, null, ex);
- } //catch (IOException ex) {
- }
- @SuppressWarnings("unchecked")
- // <editor-fold defaultstate="collapsed" desc="Generated Code">
- private void initComponents() {
- jPanel1 = new javax.swing.JPanel();
- g1 = new javax.swing.JLabel();
- setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
- jPanel1.setBackground(new java.awt.Color(255, 255, 255));
- g1.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
- g1.setIcon(newImage);
- g1.setText("GAMBAR");
- javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
- jPanel1.setLayout(jPanel1Layout);
- jPanel1Layout.setHorizontalGroup(
- jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(jPanel1Layout.createSequentialGroup()
- .addGap(162, 162, 162)
- .addComponent(g1)
- .addContainerGap(644, Short.MAX_VALUE))
- );
- jPanel1Layout.setVerticalGroup(
- jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(jPanel1Layout.createSequentialGroup()
- .addGap(58, 58, 58)
- .addComponent(g1)
- .addContainerGap(350, Short.MAX_VALUE))
- );
- javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
- getContentPane().setLayout(layout);
- layout.setHorizontalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
- );
- layout.setVerticalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
- );
- pack();
- }// </editor-fold>
- /**
- * @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(TampilGambarBlob.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (InstantiationException ex) {
- java.util.logging.Logger.getLogger(TampilGambarBlob.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (IllegalAccessException ex) {
- java.util.logging.Logger.getLogger(TampilGambarBlob.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (javax.swing.UnsupportedLookAndFeelException ex) {
- java.util.logging.Logger.getLogger(TampilGambarBlob.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 TampilGambarBlob().setVisible(true);
- }
- });
- }
- // Variables declaration - do not modify
- private javax.swing.JLabel g1;
- private javax.swing.JPanel jPanel1;
- // End of variables declaration
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement