- BLOB
- CLOB
- ARRAY
- OBJECT
BLOB -
1. BLOB stands for Binary Large Object.
2. If database table created with column-type as BLOB, it can hold binary objects like images, audio, video
3. PreparedStatement provide the following methods to manipulate BLOB type -
- void setBinaryStream(int paramindex,InputStream x,int length) - Sets the designated parameter to the given input stream, which will have the specified number of bytes.
- void setBlob(int paramindex,InputStream,long length) - Sets the designated parameter to the input stream object.
PROGRAM TO INSERT IMAGE INTO DATABASE
SQL> create table user_register(name varchar2(25), username varchar2(25),password varchar2(25),image blob);
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;
public class Test18 {
public static void main(String[] args) throws ClassNotFoundException, SQLException, FileNotFoundException {
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "scott", "rakesh");
PreparedStatement ps = con.prepareStatement("insert into user_register values(?,?,?,?)");
Scanner scan = new Scanner(System.in);
System.out.println("Input name");
String name = scan.next();
System.out.println("Input username");
String username = scan.next();
System.out.println("Input password");
String pwd = scan.next();
System.out.println("Input image");
String fname=scan.next();
FileInputStream fis = new FileInputStream(fname);
File f = new File(fname);
ps.setString(1, name);
ps.setString(2, username);
ps.setString(3, pwd);
ps.setBlob(4, fis, (int)f.length());
int count=ps.executeUpdate();
System.out.println(count);
}
}
Output -
Input name
rakesh
Input username
rakeshguwa
Input password
rocking
Input image
BC20161005_123231.jpg
1
Note : In input filename give the path of the file if it exists in your working workspace then then just give the filename.
No comments:
Post a Comment