从mySQL DB

时间:2018-04-03 11:40:52

标签: java mysql jframe

我设法显示第一张图片,但它没有显示列

中的下一个blob图像

PS。我已经仔细检查了数据库中的所有内容,即使在工作台中使用查询,代码中的查询也是正确的!它会返回我想要的内容

提前谢谢。

我的代码:

public BufferedImage image()
    {
            System.out.println("I am in Image");


            try {
                System.out.println("trying to find  com.mysql.jdbc.driver");
                Class.forName("com.mysql.jdbc.Driver");
                Connection con = DriverManager.getConnection(url, user, password);
                System.out.println("Success");

                Statement stmt = con.createStatement();
                ResultSet rs = stmt.executeQuery("select image from images where state = 0 ");

                while (rs.next())
                {

                    java.sql.Blob blob = rs.getBlob(1);
                    System.out.println(blob);
                    System.out.println(blob.length());


                    InputStream in = blob.getBinaryStream(1,blob.length());

                    System.out.println(!rs.next());
                    System.out.println(in);

                    BufferedImage image = ImageIO.read(in);
                    System.out.println(image);

                   // Display the image

                    ImageIcon icon = new ImageIcon(image);
                    JLabel lbl=new JLabel();
                    lbl.setIcon(icon);

                    JFrame frame=new JFrame();
                    frame.setLayout(new FlowLayout());
                    frame.setSize(500,800);

                    frame.add(lbl);
                    frame.setVisible(true);
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                    return image;


                }

                con.close();
                System.out.println("reached here");


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

            return null;
        }

1 个答案:

答案 0 :(得分:0)

问题是由于某种原因,在将if(!rs.next)更改为

之后它正在跳过一行
if (rs.next())
            {

                anyResults = true;

                Blob blob = rs.getBlob("image");

                id = rs.getInt("id");

                System.out.println(id);
                System.out.println(blob);
                System.out.println(blob.length());


                InputStream in = blob.getBinaryStream(1, blob.length());

现在它对我有用