简体   繁体   English

从数据库检索图像到imageView

[英]Retriving the image from DB to imageView

There is the table in DB with images. DB中有带图像的表。 I want, that an Image will show in Image View, but something is wrong and I have SQLException. 我想要一个图像将在图像视图中显示,但是出了点问题,并且我有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);
}

From the documentation for ResultSet (my emphasis): ResultSet的文档中(我的重点):

A ResultSet object maintains a cursor pointing to its current row of data. ResultSet对象维护一个游标,该游标指向其当前数据行。 Initially the cursor is positioned before the first row. 最初,光标位于第一行之前。 The next method moves the cursor to the next row 下一个方法将光标移动到下一行

So you need to call next() to point the cursor to each row. 因此,您需要调用next()将光标指向每一行。 Assuming there's at most one row in the table, or that you are only interested in the first row, you could do 假设表格中最多有一行,或者您只对第一行感兴趣,则可以

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);
}

But note the comments below the OP as well. 但也请注意OP下的注释。

Here is an example from a piece of code I wrote. 这是我编写的一段代码的示例。 This is an sqlite db 这是一个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);
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM