import java.sql.*;
import java.util.*;
import java.io.*;
public class InsertBlob
{
public static void main(String ar[])
{
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/college","root","me");
String qry = "insert into imagee values(?,?,?)";
PreparedStatement ps=con.prepareStatement(qry);
ps.setString(1,"Chetan");
ps.setString(2,"Chauhan");
File f = new File("Butterfly.jpg");
FileInputStream fis = new FileInputStream(f);
System.out.println((int)f.length());
ps.setBinaryStream(3,fis,(int)f.length());
int i = ps.executeUpdate();
System.out.println(i+ " Record inserted successfully");
}
catch(Exception e)
{
System.out.println(e);
}
}
}
...........................
To Get Image From DataBase
<%@page import="java.sql.*,java.io.*"%>
<% InputStream sImage; response.setContentType("image/jpeg"); try { Class.forName("com.mysql.jdbc.Driver"); Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/college","root","me"); Statement stmt = con.createStatement(); String qry="select * from imagee"; ResultSet rs=stmt.executeQuery(qry); if(rs.next()) { byte[] bytearray = new byte[1048576]; response.setContentLength(bytearray.length); int size=0; sImage = rs.getBinaryStream(3); //response.reset(); while((size=sImage.read(bytearray))!= -1) { response.getOutputStream().write(bytearray,0,size); } } } catch(Exception e) { System.out.println(e); } %>
No comments:
Post a Comment