/*  To Store image in database- Store.java  */
import java.io.*;
import java.sql.*;
public class Store {
public static void main(String args[])throws Exception
{Connection con=null;
     String url="";
try{
 // Step 1. Load the Type 1 database driver:
                        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                        // Step 2. Create a connection to the database
                        //         using pre-defined Data Source:
                        File f=new java.io.File("c:/Temp.accdb");
                        File c=new java.io.File(System.getProperty("user.dir")+"/Temp.accdb");
                        if(f.exists())
                            url= "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=" +f.getPath();
                        else if(c.exists())
                            url= "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=" +c.getPath();
                        con = DriverManager.getConnection(url,"","");
    File imgfile = new File("D:/semanticweb.jpg");
   if(!imgfile.exists())
imgfile=new File("semanticweb.jpg");
    FileInputStream fis = new FileInputStream(imgfile);
PreparedStatement psmnt = con.prepareStatement("INSERT INTO IMAGESTORAGE(IMAGE) VALUES(?)");
   psmnt.setBinaryStream(1, (InputStream)fis, (int)(imgfile.length()));
int insertCount= psmnt.executeUpdate();
System.out.println(insertCount);
    psmnt.close();
    con.close();
    }
catch(Exception e)
        {
System.out.print(""+e);
}
    }
}
/* To retrieve image from database and store in local directory- Retrieve.java */
import java.io.*;
import java.sql.*;
public class Retreival
{
public static void main(String args[])throws Exception
 {
 Connection con=null;
      String url="";
int c;
 // Step 1. Load the Type 1 database driver:
                        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                        // Step 2. Create a connection to the database
                        //         using pre-defined Data Source:
                        File f=new java.io.File("c:/Temp.accdb");
                        File ff=new java.io.File(System.getProperty("user.dir")+"/Temp.accdb");
                        if(f.exists())
                            url= "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=" +f.getPath();
                        else if(ff.exists())
                            url= "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=" +ff.getPath();
                        con = DriverManager.getConnection(url,"","");
 PreparedStatement psmnt = con.prepareStatement("SELECT IMAGE FROM IMAGESTORAGE");
 ResultSet rs = psmnt.executeQuery(); 
  if(rs.next())
  {
  InputStream fis1;
  FileOutputStream fos;
  fis1 = rs.getBinaryStream("image");
  fos = new FileOutputStream(new File("SunsetImageRead.jpg"));
  while ((c = fis1.read()) != -1) 
  {
  fos.write(c);
  }
  fis1.close();
  fos.close();
  }
  } 
}
Click here to download Complete workspace (database,image,Retrieve.java, Store.java)
 
No comments:
Post a Comment