how to retrieve image from mysql database in jave using id

Solutions on MaxInterview for how to retrieve image from mysql database in jave using id by the best coders in the world

showing results for - "how to retrieve image from mysql database in jave using id"
Walid
02 May 2019
1<%@ page import="java.sql.*"%>
2<%@ page import="java.io.*"%>
3<%
4Blob image = null;
5Connection con = null;
6byte[ ] imgData = null ;
7Statement stmt = null;
8ResultSet rs = null;
9try {
10Class.forName("com.mysql.jdbc.Driver");
11con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb","","");
12stmt = con.createStatement();
13rs = stmt.executeQuery("select image from image where id = '1'");
14if (rs.next()) {
15image = rs.getBlob(1);
16imgData = image.getBytes(1,(int)image.length());
17}
18else {
19out.println("Display Blob Example");
20out.println("image not found for given id>");
21return;
22}
23// display the image
24response.setContentType("image/jpg");
25OutputStream o = response.getOutputStream();
26o.write(imgData);
27o.flush();
28o.close();
29} catch (Exception e) {
30out.println("Unable To Display image");
31out.println("Image Display Error=" + e.getMessage());
32return;
33} finally {
34try {
35rs.close();
36stmt.close();
37con.close();
38} catch (SQLException e) {
39e.printStackTrace();
40}
41}
42%>