Java PreparedStatement.setBlob(int parameterIndex, Blob x)
Syntax
PreparedStatement.setBlob(int parameterIndex, Blob x) has the following syntax.
void setBlob(int parameterIndex, Blob x) throws SQLException
Example
In the following code shows how to use PreparedStatement.setBlob(int parameterIndex, Blob x) method.
// www . ja va 2 s . co m
import java.io.ObjectOutputStream;
import java.sql.Blob;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import javax.swing.ImageIcon;
public class Main {
public static void main(String[] args) throws Exception {
Connection con;
con = DriverManager.getConnection("jdbc:derby://localhost:1527/" + "c:\\db\\employee");
PreparedStatement ps;
ps = con.prepareStatement("insert into employee(name,photo) " + "values(?,?)");
ps.setString(1, "Duke");
Blob blob = con.createBlob();
ImageIcon ii = new ImageIcon("duke.png");
ObjectOutputStream oos;
oos = new ObjectOutputStream(blob.setBinaryStream(1));
oos.writeObject(ii);
oos.close();
ps.setBlob(2, blob);
ps.execute();
blob.free();
ps.close();
}
}