0

I am trying to display image from database.I can save image from database with below codes.But now, I don't want to save image I want to display image in xhtml file.I can't do this.How can I do?

public class ReadBlobDemo {
        public static void main(String[] args) 
    {
            DB db = new DB();
            Connection conn=db.dbConnect(
              "jdbc:mysql://localhost/student","root","");
            db.getImageData(conn);
    }
}
class DB
{
    public DB() {}
       public Connection dbConnect(String db_connect_string,
       String db_userid, String db_password){
            try{
                    Class.forName("com.mysql.jdbc.Driver");
                    Connection conn = DriverManager.getConnection(
                      db_connect_string, db_userid, db_password);
                      return conn;                        
            }
            catch (Exception e){
                    e.printStackTrace();
                    return null;
            }
    }
public void getImageData(Connection conn){
         byte[] fileBytes;
         String query;
         try{
                 query = "select photo from person";
                 Statement state = conn.createStatement();
                 ResultSet rs = state.executeQuery(query);
                 if (rs.next()) {
                          fileBytes = rs.getBytes(1);
                          OutputStream targetFile=  
                          new FileOutputStream(
                               "d://new.JPG");

                          targetFile.write(fileBytes);
                          targetFile.close();
                }        

         }
         catch (Exception e){
                 e.printStackTrace();
         }
}
};
mery
  • 23
  • 1
  • 7
  • The tag `[blob]` is worth reading [this](http://stackoverflow.com/q/8207325/1391249). Just pass a byte array here `new DefaultStreamedContent(new ByteArrayInputStream(...))`. – Tiny Sep 28 '15 at 12:46
  • Nothing in the question indicates that OP is using PrimeFaces. – BalusC Sep 28 '15 at 15:42
  • @BalusC : I missed the bus. :) – Tiny Sep 28 '15 at 16:01

0 Answers0