JDBC Retrieve Images from FDatabase

Advertisements

JDBC Insert Image JDBC Save Point

Retrieve Images from DataBase in JDBC

While retrieving a image from a database, the binary data of image will be selected from the database.

retrieve image from databse

Important Points

  • Again we need to convert binary data to image, because in database binary data of image is store.
  • When we select image from database, it will be store in a ResultSet object..
  • From ResultSet object we need to read the binary data and we need to store in a InputStream object.

Convert binary data into image

To convert binary data into a image again we write the data into a file using FileOutputStream object.

Example to Retrieve Image from database

import java.sql.*;
import java.util.*;
import java.io.*;
class PhotoSelect 
{
Connection con;
public void openCon()throws Exception
{
Class.forName("oracle.jdbc.OracleDriver");
con=DriverManager.getConnection("jdbc:oracle:thin:@rama-pc:1521:xe","system","system");
System.out.println("connection is opened");
}
public void select()throws Exception
{
Scanner s=new Scanner(System.in);
PreparedStatement pstmt=con.prepareStatement("select photo from emp_info where empid=?");
System.out.println("enter emp id");
int empid=s.nextInt();
pstmt.setInt(1,empid);
ResultSet rs=pstmt.executeQuery();
rs.next();
InputStream is=rs.getBinaryStream(1);
rs.close();
FileOutputStream fos=new FileOutputStream("c:/img001.gif");
int k;
while((k=is.read())!=-1)
{
fos.write(k);
}
System.out.println("picture is ready open c:drive");
pstmt.close();
fos.close();
}//end of
public void closeCon()throws Exception
{
con.close();
}//end of select

public static void main(String[] args)throws Exception 
{
PhotoSelect ps= new PhotoSelect();
ps.openCon();
ps.select();
ps.closeCon();
 }//end of main
}//end of class

JDBC Insert Image JDBC Save Point

Google Advertisment

Buy This Ad Space @$20 per Month, Ad Size 600X200 Contact on: hitesh.xc@gmail.com or 9999595223

Magenet is best Adsense Alternative here we earn $2 for single link, Here we get links ads. Magenet

For Projects 9999595223

Google Advertisements


Buy Websites 9999595223

Buy College Projects with Documentation Contact on whatsapp 9999595223. Contact on: hitesh.xc@gmail.com or 9999595223 Try this Keyword C++ Programs

Advertisements