【问题标题】:Can't get image from Mysql blob column无法从 Mysql blob 列获取图像
【发布时间】:2018-10-23 00:45:01
【问题描述】:

我无法从 Mysql 数据库中的 blob 列获取图像到 jlabel。这是我的代码:

pst = con.prepareStatement("SELECT `profilepic` FROM `librarian` WHERE `username` = `Tharindu`");
rs = pst.executeQuery();
BufferedImage im = ImageIO.read(rs.getBinaryStream(1));
ImageIcon image = new ImageIcon(im);
jlabelpic.setIcon(image);

例外:

java.sql.SqlException: Before start of result set

如何添加图片?

【问题讨论】:

    标签: sql image netbeans


    【解决方案1】:

    您面临的问题不是图像的实际加载,而是从数据库中检索图像。要指向结果集的右行,您首先需要调用 rs.next(); 将指针移至下一行,因为最初指针位于“-1”行中,因此就在结果集之前。

    有关它的更多信息,此帖子也可能会有所帮助:java.sql.SQLException: Before start of result set

    作为代码,它可能看起来像这样:

    pst = con.prepareStatement("SELECT `profilepic` FROM `librarian` WHERE `username` = `Tharindu`");
    rs = pst.executeQuery();
    if(rs.next()){
        BufferedImage im = ImageIO.read(rs.getBinaryStream(1));
        ImageIcon image = new ImageIcon(im);
        jlabelpic.setIcon(image);
    }
    

    希望对你有帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-03-05
      • 1970-01-01
      • 2010-10-23
      • 1970-01-01
      • 2015-07-28
      • 2018-11-01
      • 2014-08-20
      • 1970-01-01
      相关资源
      最近更新 更多