2013-11-04 49 views
0

我试图显示图像从数据库到jlabel IMAGES =列名,currentuser.getText()=我有一个文本上面这决定了我的数据库列USERNAME我的数据库中存储的图像(Longblob),我无法显示它到jlabel

编辑 - 我真的不知道该怎么办了,它是一个星期,但我依然不能从数据库

public class MyProfile extends JFrame implements ActionListener{ 
Container c; 
ResultSet rs; 
Connection con; 
Statement st; 
TempData temp = new TempData(); //Class for storing current user who logins (Set & get) 
JLabel currentuser = new JLabel("" + temp.getUsername()); 
JLabel displayPhoto = new JLabel(); 
public MyProfile() { 
    super("My Profile"); 
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    this.setSize(1175, 698); 
    this.setLocationRelativeTo(null); 
    this.setVisible(true) 
    c = this.getContentPane(); 
    c.setLayout(null); 

    try { 
     Class.forName("com.mysql.jdbc.Driver"); 
     con = DriverManager.getConnection("jdbc:mysql://localhost/pancakefinder", "root", ""); 
     st = con.createStatement(); 
    } catch (Exception exp) { 
    } 

    c.add(currentuser); 
    currentuser.setFont(new Font("Times New Roman", Font.PLAIN, 20)); 
    currentuser.setForeground(Color.orange); 

    c.add(displayPhoto); 
    displayPhoto.setBounds(160, 330, 250, 230); 
    displayPhoto(); 
} 

public void displayPhoto() { 
    try { 
     PreparedStatement pst = null; 
     rs = null; 
     pst = con.prepareStatement("select IMAGES from images where USERNAME = '" + currentuser.getText() + "'"); 
     rs = pst.executeQuery(); 

     byte[] bytes = null; 
     if (rs.next()) { 
      bytes = rs.getBytes("images"); 
      Image img = Toolkit.getDefaultToolkit().createImage(bytes); 
      displayPhoto.setIcon(new ImageIcon((img))); 
      displayPanel.add(displayPhoto); 
     } 

    } catch (SQLException e) { 

     e.printStackTrace(); 
    } 


} 
public static void main(Stringp[] args){ 
MyProfile ex = new MyProfile(); 

} }

+0

“*我无法显示图像*”不是一个问题描述,它可以帮助您。你有错误还是异常?如果是,哪一个?图像是否显示?图像是否损坏?你得到了什么? –

+0

没有错误或任何异常,但图像还没有,损坏?我想不是我尝试上传新图片,但仍然没有。我在周三的演示文稿,这是我需要的最后一部分:( – Alvinrightback

+0

我试过这个 if(rs.next()); InputStream binaryStream = new BufferedInputStream(rs.getBinaryStream(“IMAGES”)); image = ImageIO .read(binaryStream); displayPhoto.setIcon(new ImageIcon(image)); and nullpointerexception – Alvinrightback

回答

0
显示图像

我猜这是displayPanelJPaneldisplayPhotoJLabel。如果在pack()之前加displayPhoto,setIcon()就够了。如果没有,你需要在add()之后revalidate()面板。同时检查rs.next()结果是否为false,并记住它只会取一行;你需要一个循环来获得其他行。

相关问题