0% found this document useful (0 votes)
44 views1 page

Traer Imagen en JSP PDF

This Java code retrieves an image from a MySQL database and displays it. It establishes a connection to the database, executes a prepared statement to select an image with a specific ID, gets the image as a binary stream, writes it to the response output stream, and closes all connections.

Uploaded by

Jose Merino
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views1 page

Traer Imagen en JSP PDF

This Java code retrieves an image from a MySQL database and displays it. It establishes a connection to the database, executes a prepared statement to select an image with a specific ID, gets the image as a binary stream, writes it to the response output stream, and closes all connections.

Uploaded by

Jose Merino
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

retrieve_image.

jsp Printed on 30/07/2008, 9:19:31 Page 1


1 <%@page import="java.sql.*" %>
2 <%@page import="java.io.*" %>
3
4
5 <%
6 // declare a connection by using Connection interface
7 Connection connection = null;
8
9 /* Create string of connection url within specified format with machine
10 name, port number and database name. Here machine name id localhost
11 and database name is student. */
12
13 String connectionURL = "jdbc:mysql://localhost:3306/mahendra";
14
15 /*declare a resultSet that works as a table resulted by execute a specified
16 sql query. */
17 ResultSet rs = null;
18
19 // Declare statement.
20 PreparedStatement psmnt = null;
21
22 // declare InputStream object to store binary stream of given image.
23 InputStream sImage;
24
25 try {
26
27 // Load JDBC driver "com.mysql.jdbc.Driver"
28 Class.forName("com.mysql.jdbc.Driver").newInstance();
29
30 /* Create a connection by using getConnection() method that takes
31 parameters of string type connection url, user name and password to
32 connect to database. */
33 connection = DriverManager.getConnection(connectionURL, "root", "root");
34
35 /* prepareStatement() is used for create statement object that is
36 used for sending sql statements to the specified database. */
37 psmnt = connection.prepareStatement("SELECT image FROM save_image WHERE id = ?");
38
39 psmnt.setString(1, "11");
40 rs = psmnt.executeQuery();
41 if(rs.next()) {
42 byte[] bytearray = new byte[1048576];
43 int size=0;
44 sImage = rs.getBinaryStream(1);
45 //response.reset();
46 response.setContentType("image/jpeg");
47
48 while((size=sImage.read(bytearray))!= -1 ){
49
50 response.getOutputStream().write(bytearray,0,size);
51 }
52 }
53 }
54 catch(Exception ex){
55 out.println("error :"+ex);
56 }
57
58 finally {
59 // close all the connections.
60 rs.close();
61 psmnt.close();
62 connection.close();
63 }
64
65 %>
66
C:\retrieve_image.jsp File date: 12/07/2008 File time: 17:52:58

You might also like