Develop Java application to store image in a database as well as retrieve image from database Create Table SQL Command: “create table(name char(10), img image)

CODE:

store.java

 

package imagesr;

import java.sql.*;

import java.io.*;

 

public class ImageSR {

 

    public static void main(String[] args) {

        try{

            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");

            Connection con = DriverManager.getConnection("jdbc:sqlserver://localhost:5000;databaseName=gv","sa","gv30");

            PreparedStatement ps= con.prepareStatement("insert into imgtable2 values(?,?)");

            ps.setString(1, "KIRAN");

            FileInputStream fin = new FileInputStream("D:\\t.jpg");

            ps.setBinaryStream(2, fin,fin.available());

            int i = ps.executeUpdate();

            con.close();

            System.out.println("Record Inserted Successfully.");

        }

        catch(Exception e){

            System.out.println(e);

        }

    }

    

}

 

retrieve.java

 

package imagesr;

import java.sql.*;

import java.io.*;

public class Retrieve {

    public static void main(String[] args)

    {

        try

        {

            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");

            Connection con=DriverManager.getConnection("jdbc:sqlserver://localhost:5000;databaseName=gv","sa","gv30");

            PreparedStatement ps=con.prepareStatement("select * from imgtable2");  

            ResultSet rs=ps.executeQuery();  

            if(rs.next())

            {//now on 1st row  

            

                Blob b=rs.getBlob(2);//2 means 2nd column data  

                byte barr[]=b.getBytes(1,(int)b.length());//1 means first image  

              

                FileOutputStream fout=new FileOutputStream("E:\\image\\output.jpg");  

                fout.write(barr);  

              

                fout.close();  

            }//end of if  

            System.out.println("Done");  

              

            con.close();  

        }

        catch(Exception e)

        {

            

        }

    }

}

OUTPUT:

Develop Java application to store image in a database as well as retrieve image from database Create Table SQL Command: “create table(name char(10), img image)




Post a Comment

0 Comments