Youtube Channel

Storing/Fetching images to databases

class StoreStuff extends oracle.jdbc.driver.OracleDriver
{
public static void main(String[] args)
{
try
{
Connection con=DriverManager.getConnection(“jdbc:odbc:thin:@localhost@1521:orcl”,”scott”,”tiger”);
PreparedStatement pst=con.prepareStatement(“insert into empall values(?,?,?,?)”);
File f=new File(“myphot.gif”);
FileInputStream fis=new FileInputStream(f);
pst.setInt(1,10);
pst.setString(2,”Kamlesh”);
pst.setString(3,”Hydrabad”);
pst.setBLOB(4,fis,(int)f.length());
int res=pst.executeUpdate();
System.out.prinln(“image Inserted”);
pst.close();
fis.close();
con.close();
}
catch (Exception e)
{
System.err.println(e);
}
}
}

Retriving stuffs form database
class RetriveStuff extends oracle.jdbc.driver.OracleDriver
{
public static void main(String[] args)
{
try
{
Connection con=DriverManager.getConnection(“jdbc:oracle:thin:@localhost:1521:orcl”,”system”,”tiger”);
Statement st=con.createStatement();
ResultSet rs=st.executeQuery(“select * from empall”);
InputStream is=null;
if(rs.next())
{
is=rs.getBinaryStream(“photo”);//column name or index
}
byte buffer[]=new byte[4096];
int byteread=0;
while(byteread=is.read(buffer))!=-1)
{
fos.write(buffer,0,byteread);
}
System.out.println(“image retrived”);
is.close();
fos.close();
rs.close();
st.close();
}
catch (Exception e)
{
System.out.println(e);
}
}
}
Next PostNewer Post Previous PostOlder Post Home