簡體   English   中英

從數據庫檢索圖像到imageView

[英]Retriving the image from DB to imageView

DB中有帶圖像的表。 我想要一個圖像將在圖像視圖中顯示,但是出了點問題,並且我有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);
}

ResultSet的文檔中(我的重點):

ResultSet對象維護一個游標,該游標指向其當前數據行。 最初,光標位於第一行之前。 下一個方法將光標移動到下一行

因此,您需要調用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下的注釋。

這是我編寫的一段代碼的示例。 這是一個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