Take a look at the following code snippet that I use to extract images from a database:
response.setContentType("image/gif");
String url="jdbc:oracle:thin:@localhost:1521:xe";
String username="xyz";
String password="abc";
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection conn=DriverManager.getConnection(url,username,password);
String sql="Select name,description,image from pictures";
PreparedStatement stmt=conn.prepareStatement(sql);
ResultSet resultSet=stmt.executeQuery();
ServletOutputStream sos=response.getOutputStream();
while(resultSet.next()) {
byte[] buffer=new byte[1];
InputStream is=resultSet.getBinaryStream(3);
while(is.read(buffer)>0){
sos.write(buffer);
}
sos.println();
sos.flush();
}
sos.close();
conn.close();
I am trying to have this code display images that are retrieved from the database. This code should extract multiple images from multiple lines that are stored in the database. But this code displays a single image.
source
share