【问题标题】:Retriving the image from DB to imageView将图像从 DB 检索到 imageView
【发布时间】:2017-10-14 12:08:35
【问题描述】:

数据库中有一张带有图片的表格。我想要一个图像将显示在图像视图中,但是出了点问题,我有 SQLException。

 ConnectionHelper ch=new ConnectionHelper();
    ch.Connect();
    String q="SELECT IMG FROM img_ins";

    Statement st=ch.con.createStatement();
    ResultSet rs = st.executeQuery(q);

   InputStream is= rs.getBinaryStream("IMG");
    OutputStream os=new FileOutputStream(new File("img.jpg"));
    byte [] content= new byte[1024];
    int size=0;


        while ((size=is.read(content))!=-1){

            os.write(content, 0, size);
        }

    os.close();
    is.close();

    javafx.scene.image.Image image1=new Image("file:img.jpg", image.getFitWidth(), image.getFitHeight(), true, true);
    image.setImage(image1);
    image.setPreserveRatio(true);
}

【问题讨论】:

  • 在问题中发布完整的堆栈跟踪。
  • 当你可以create an Image directly from an InputStream时,你为什么要写入文件?
  • 第一:直接将图像添加到数据库不是一个好方法。第二:将其读取为 blob
  • 您的表格包含哪些信息?图像名称或图像目录...等?
  • @kodmanyagha 对不起,我是这个领域的新手..你能解释一下添加图像的更好方法吗?我试着把它读成 blob.. 但我也没有收到好的结果..

标签: java mysql sql javafx


【解决方案1】:

来自ResultSet 的文档(我的重点):

ResultSet 对象维护一个指向其当前数据行的游标。 最初光标位于第一行之前。 next 方法将光标移动到下一行

所以你需要调用next() 将光标指向每一行。假设表中最多有一行,或者您只对第一行感兴趣,您可以这样做

ConnectionHelper ch=new ConnectionHelper();
ch.Connect();
String q="SELECT IMG FROM img_ins";

Statement st=ch.con.createStatement();
ResultSet rs = st.executeQuery(q);

if (rs.next()) {
    InputStream is= rs.getBinaryStream("IMG");

    // instead of the next 9 lines, you could just do
    // javafx.scene.image.Image image1 = new Image(is);

    OutputStream os=new FileOutputStream(new File("img.jpg"));
    byte [] content= new byte[1024];
    int size=0;


        while ((size=is.read(content))!=-1){

            os.write(content, 0, size);
        }

    os.close();
    is.close();

    javafx.scene.image.Image image1=new Image("file:img.jpg", image.getFitWidth(), image.getFitHeight(), true, true);
    image.setImage(image1);
    image.setPreserveRatio(true);
}

但也要注意 OP 下面的 cmets。

【讨论】:

  • 感谢您的回答,但它不起作用......我现在没有任何例外,但图像未显示。
  • @Roshki 这是一个完全不同的问题:您没有发布任何与显示 ImageView 相关的代码。
  • 我使用Scene Builder并通过“public ImageView image;”插入ImageView
  • @Roshki 同样,这与您发布的问题无关(这是关于从数据库中检索图像,而不是在 UI 中显示图像)。如果您对图像进行硬编码而不是从数据库加载,它会起作用吗?如果没有,那么您应该发布一个新问题。
【解决方案2】:

这是我编写的一段代码中的一个示例。这是一个 sqlite 数据库

try (Connection conn = DriverManager.getConnection("jdbc:sqlite:contactFX.db");
             Statement stmt = conn.createStatement(); 
             ResultSet rset = stmt.executeQuery(sqlTCNote)) 
        {
            while(rset.next())
            {                
                Company c1 = new Company();  
                c1.setID(Integer.toString(rset.getInt("company_id")));
                c1.setName(rset.getString("company_name"));
                .
                .
                .
                c1.setWebSiteAddress(rset.getString("website_address"));  

                //This part is important to you.
                InputStream input = rset.getBinaryStream("image");
                InputStreamReader inputReader = new InputStreamReader(input);                        
                if(inputReader.ready())
                {
                    File tempFile = new File("tempFile.jpg");

                    FileOutputStream fos = new FileOutputStream(tempFile);
                    byte[] buffer = new byte[1024];
                    while(input.read(buffer) > 0){
                        fos.write(buffer);                        
                    }
                    Image image = new Image(tempFile.toURI().toURL().toString());
                    c1.setImage(image);//right here is where you want to set your imageView with the image.
                }     
                companyData.add(c1);
            }              
        }
        catch(SQLException | IOException ex)
        {
            Logger.getLogger(MainController.class.getName()).log(Level.SEVERE, null, ex);
        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-12-26
    • 1970-01-01
    • 1970-01-01
    • 2011-08-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多