![java读取图片插入blob,并取出 - 饶为 - 饶为先生的博客 java读取图片插入blob,并取出 - 饶为 - 饶为先生的博客](http://img1.ph.126.net/sghqMZjVxeTnT8QV88WLAA==/6632323407977875678.png)
这是插入数据库的方法。详细内容查看注解。public class InsertImage{
//将读取的图片插入数据库中
public static void insertImg() throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException, IOException{
//JDBC连接格式
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
String url="jdbc:oracle:thin:@localhost:1521:orcl"; //ORCL 是sid
String user="soa";
String password="soa";
Connection con = DriverManager.getConnection(
url,user, password);
con.setAutoCommit(false);
//切近!!!无论是blob还是clob,在插入前都需要先插入一个空的blob或则clob内容
PreparedStatement pstmt = con.prepareStatement("insert into BLOB(BLOB_T) values(empty_blob())");
pstmt.executeUpdate();
pstmt.close();
//使数据库处于可编辑状态
String sql1 = "select * from BLOB for update";
ResultSet rs=null;
rs=con.createStatement().executeQuery(sql1);
while(rs.next()){
//取到数据库中blob字段
oracle.sql.BLOB b=(oracle.sql.BLOB )rs.getBlob("BLOB_T");
//文件路径
File f=new File("C:\\Users\\Administrator\\Desktop\\1.png");
//用输入流接收文件内容
BufferedInputStream in=new BufferedInputStream(new FileInputStream(f));
//用输出流得到blob的二进制文件流
BufferedOutputStream out=new BufferedOutputStream(b.getBinaryOutputStream());
int c;
while ((c=in.read())!=-1) {
try {
//将输入流接收到的,写入输出流
out.write(c);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
in.close();
out.close();
}
//关闭,提交
con.commit();
}
public static void readImg() throws SQLException, InstantiationException, IllegalAccessException, ClassNotFoundExcep