Hi Friend,
We are providing you the code where we have used MySql database.
Use the following code:
<%@page import="java.io.*"%>
<%@page import="java.sql.*"%>
<%
String connectionURL = "jdbc:
mysql://localhost:3306/test";;;
Connection con=null;
Statement stmt = null;
ResultSet rs = null;
Blob image = null;
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:
mysql://localhost:3306/test","root","root";;);
stmt = con.createStatement();
rs = stmt.executeQuery("select image from image where image_id =1");
if (rs.next()) {
image = rs.getBlob(1);
}
File f=new File("C:\\newImage.jpg");
FileOutputStream fos=new FileOutputStream(f);
InputStream in = image.getBinaryStream();
int length = (int) image.length();
int bufferSize = 1024;
byte[] buffer = new byte[bufferSize];
while ((length = in.read(buffer)) != -1) {
fos.write(buffer, 0, length);
}
out.println("Image is saved into the file.");
in.close();
fos.close();
} catch (Exception e){}
finally {
try {
rs.close();
stmt.close();
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
%>
Hope that it will be helpful for you.
Thanks