By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,949 Members | 2,038 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,949 IT Pros & Developers. It's quick & easy.

storing image in database...

P: 10
i have some problem on ilplementing the code for the insertion of image into database that didnot get how to use the code for image storege.so can u help me in the use for the code by which i want to store the image of teacher with their respective database and i also have to retrive the same when i want to see the details of teacher..here is the code-

import java.io.*;
import java.sql.*;
import java.util.*;

public class StoreFile
{
Connection con=null;
DataBase db = null;
String filename=null;
String filedesc=null;

public StoreFile()
{

}

public StoreFile(String filedesc,String filename)
{
this.filedesc = filedesc;
this.filename=filename;
}

public void storeFile() throws Exception
{
System.out.println("Before DataBase Connection ");
PreparedStatement pst = null;
try
{
db = new DataBase();
con = db.getConnection();
pst = con.prepareStatement("insert into files values(?,?)");
pst.setString(1,filedesc);
FileInputStream fis=new FileInputStream(filename);
byte[] b= new byte[fis.available()+1];
fis.read(b);
pst.setBytes(2,b);
pst.execute();
System.out.println("File Stored Successfully");
}
catch(Exception e)
{
System.out.println(" Problem : "+e);
}
finally
{
con.close();
pst.close();
}

}

public void getFile(String filename) throws Exception
{
System.out.println("Before DataBase Connection ");
PreparedStatement pst = null;
ResultSet rs=null; try
{
db = new DataBase();
con = db.getConnection();
pst = con.prepareStatement("select filetype from files where filename=?");
pst.setString(1,filename);
rs=pst.executeQuery();
while(rs.next())
{
byte[] b=rs.getBytes("filetype");
FileOutputStream fos=new FileOutputStream(new File("new1.html"));
fos.write(b);
}
System.out.println("File Written Successfully");
}
catch(Exception e)
{
System.out.println(" Problem : "+e);
}
finally
{
con.close();
pst.close();
}

}

/*dbStatement = dbConnection.prepareStatement("select * from photos ");
rs=dbStatement.executeQuery();
if (rs.next()==true)
{
b=rs.getBytes("photo");
FileOutputStream fos=new FileOutputStream(new File("Anil.jpg"));
fos.write(b);
}*/


public static void main(String args[]) throws Exception
{
//System.out.println("What is The File Description ");
//String filedesc = new DataInputStream(System.in).readLine();
System.out.println("What is The File Name");
String filename = new DataInputStream(System.in).readLine();
//StoreFile store = new StoreFile(filedesc,filename);
//store.storeFile();
StoreFile store = new StoreFile();
store.getFile(filename);
}

}

please help me as soon as posible...
Mar 31 '07 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.