Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in /home/purvgwtw/public_html/tutorials/common.php on line 7
Retrieve image from the database using JSP.
Sponsered Links
Categories
Sponsered Links

Retrieve image from the database using JSP.

This section demonstrates you how to retrieve an image from the database and display it on the browser.

<%@ page import="java.sql.*" %>
<%@ page import="java.io.*" %>
<%

try{
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root", "root");
Statement st=con.createStatement();
ResultSet rs = st.executeQuery("select image from image where id=1");
if(rs.next()){
String imgLen = rs.getString(1);
int len = imgLen.length();
byte [] rb = new byte[len];
InputStream is = rs.getBinaryStream(1);
int index=is.read(rb, 0, len);
response.reset();
response.setContentType("image/jpg");
response.getOutputStream().write(rb,0,len);
response.getOutputStream().flush();
}
}
catch (Exception e){
e.printStackTrace();
}

%>

Sponsered Links
Latest Updates
 
All Content of this site is for learning only. We do not warrant the correctness of its content. The risk from using it lies entirely with the user. While using this site, you agree to have read and accepted our terms of use and privacy policy.
Copyright © 2014 PURVACOMPUTERS.COM All Right Reserved