2013-04-25 50 views
3

我只是想轉換一個blob的字符串,可以在數據庫中,以字節數組,然後將其轉換後轉換爲緩衝的圖像,然後將其分配給一個標籤 這裏是我的代碼轉換的blob圖像流,並將其分配到的JLabel

package ims.project; 
import java.sql.*; 
import javax.swing.*; 
import java.awt.*; 
import java.awt.image.BufferedImage; 
import java.io.ByteArrayInputStream; 
import javax.imageio.ImageIO; 

public class readingdata extends JFrame { 
    readingdata() { 
     JPanel pane = new JPanel(); 

     JLabel label1 = new JLabel("help"); 
     JLabel label2 = new JLabel("33"); 
     pane.add(label1); 
     pane.add(label2); 


     setVisible(true); 
     add(pane); 


     try { 
      Class.forName("com.mysql.jdbc.Driver"); 
      Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/ims1", "root", "root"); 
      Statement st = con.createStatement(); 
      String ss = "select Pic from supplier where Supplier_id= '" + label2.getText() + "'"; 
      JOptionPane.showMessageDialog(null, label2.getText()); 

      ResultSet rs = st.executeQuery(ss); 
      while (rs.next()) { 
       Blob blob = rs.getBlob("Pic"); 

       int blobLength = (int) blob.length(); 

       byte[] blobAsBytes = blob.getBytes(1, blobLength); 
       final BufferedImage bufferedImage = ImageIO.read(new ByteArrayInputStream(blobAsBytes)); 

       label2.setIcon(new ImageIcon(bufferedImage)); 




      } 
     } catch (Exception ex) { 
      ex.printStackTrace(); 

     } 
    } 
    public static void main(String args[]) { 
     new readingdata(); 
    } 
} 

但是當我運行這段代碼是顯示空指針堆棧追蹤

java.lang.NullPointerException 
    at javax.swing.ImageIcon.<init>(ImageIcon.java:228) 
    at ims.project.readingdata.<init>(readingdata.java:47) 
    at ims.project.readingdata.main(readingdata.java:60) 
+0

您可以添加行號嗎?或者標記哪些線是47和60? – Fildor 2013-04-25 10:21:35

+0

[使用的InputStream](http://docs.oracle.com/cd/B19306_01/java.102/b14355/jstreams.htm)在說明書 – mKorbel 2013-04-25 10:22:27

+0

ImageIcon.java:228對象,Java代碼例如鄰= image.getProperty( 「評論」,imageObserver); – user2319205 2013-04-25 10:23:27

回答

1

請嘗試以下的一段代碼:

Connection connection = null; 
PreparedStatement statement = null; 

ResultSet result; 


public DisplayImage() { 
    super("Image Display"); 
    setSize(600,600); 
    connection = getConnection(); 
    try { 
     statement = connection.prepareStatement("select content from image where id=1"); 
     result = statement.executeQuery(); 

      byte[] image = null; 
      while(result.next()) { 
       image = result.getBytes("content"); 

      } 
      Image img = Toolkit.getDefaultToolkit().createImage(image); 
      ImageIcon icon =new ImageIcon(img); 
      JLabel lPhoto = new JLabel(); 
      lPhoto.setIcon(icon); 
      add(lPhoto); 

    } catch (SQLException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

    setVisible(true); 
} 


public Connection getConnection() { 
    Connection connection = null; 

    try { 
     Class.forName("com.mysql.jdbc.Driver"); 
     connection = DriverManager.getConnection(
       "jdbc:mysql://localhost:3306/db_name", "user", "pass"); 
    } catch (Exception e) { 
     System.out.println("Error Occured While Getting the Connection: - " 
       + e); 
    } 
    return connection; 
} 

public static void main(String[] args) { 
    new DisplayImage(); 
} 

} 
相關問題