- ResultSet provide the following method to read binary data or BLOB column.
- InputStream getBinaryStream(int columnindex) - Retrieves the value of the designated column in the current row of this ResultSet object as a stream of uninterpreted bytes.
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class Test19 {
public static void main(String[] args) throws ClassNotFoundException, SQLException, IOException {
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "scott", "rakesh");
PreparedStatement ps = con.prepareStatement("select * from user_register");
ResultSet rs = ps.executeQuery();
rs.next();
FileOutputStream fos = new FileOutputStream("abc.jpg");
String name = rs.getString(1);
String username = rs.getString(2);
String pwd = rs.getString(3);
InputStream is = rs.getBinaryStream(4);
System.out.println(name);
System.out.println(username);
System.out.println(pwd);
int x;
while((x=is.read())!=-1)
{
fos.write(x);
}
}
}
Output -
rakesh
rakeshguwa
rocking
Note - The image fetched from the database will be visible in your working workspace by the name abc.jpg
No comments:
Post a Comment