文字和图像不显示

时间:2019-05-20 17:46:06

标签: mysql javafx scenebuilder

我正在尝试从数据库中检索文本和图像(描述表:包含img和文本作为字段) 并以fxml格式放置它们,但是图像和文本不想显示,但是不幸的是它没有显示任何错误,我使用label(id:text)和ImageView(id:image),请有人帮忙我

这是我的mvc:

public class Voirplus {
        private Connection connection;
    //Database connection parameters
       String url = "jdbc:mysql://localhost/database";
       String username = "root";
       String password = "";


       public voirplusform VoirPlus(String id) throws SQLException, IOException{

            ResultSet resultset;
            voirplusform c = new voirplusform() ;
            try {
                connection = (Connection) DriverManager.getConnection(url, username, password);
            } catch (SQLException e) {
                System.out.println("Error creating connection to database");
                System.exit(-1);
            }
            System.out.println("tout va bien");
            String sql = "select img, text from description where id=? ";

            try (PreparedStatement statement = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)) {

                statement.setString(1,id);
                resultset=statement.executeQuery();
                while(resultset.next())
                {
                    InputStream is= resultset.getBinaryStream("img");
                    c.txt = resultset.getString("text");
                    System.out.println(c.txt);
                    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();
                        Image imagex = new Image("file:photo.jpg",250,250,true,true);  
                        c.img = imagex;
                        }
            } catch (SQLException e) {

                System.out.println("Error creating cs");
            }

            try {
                connection.close();
                connection = null;
            } catch (SQLException e) {
                System.out.println("Error closing connection");
            }

            return (new voirplusform(c.img,c.txt));
        }

}

public class VoirplusController implements Initializable {

    @FXML

    private ImageView image;
    @FXML
    private Label text;
    public void initialize(URL location,ResourceBundle resources)
    {
        try{
            String id = "1";

            Voirplus db = new Voirplus();
             voirplusform x = db.VoirPlus(id);
             System.out.println(x.txt);
             //JOptionPane.showMessageDialog(null, x.txt);
            image.setImage(x.img);
            //image.setCache(true);
            text.setText(x.txt);


        }
        catch (Exception e){
            System.out.println();
        }
    }


}


    public class voirplusform  {
        public Image  img;
        public String txt;
        voirplusform(Image img , String txt)
        {
            this.img = img;
            this.txt = txt;
        }
        public voirplusform()
        {
            this.img = null;
            this.txt = null;
        }
    }

0 个答案:

没有答案