使用JDBC从数据库显示图像

时间:2015-09-28 12:40:23

标签: database jsf jdbc database-connection blob

我正在尝试从数据库中显示图像。我可以用以下代码保存数据库中的图像。但是现在,我不想保存图像我想在xhtml文件中显示图像。我不能这样做。如何我可以吗?

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

0 个答案:

没有答案