/*
把图片存入类型为BLOB大数据类型的表中!
*/
package com.test;
import java.sql.*;
import oracle.sql.*;
import java.io.*;
public class H4
{
public static void main(String[] args) throws Exception
{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@192.168.6.6:1521:orcl","scott","tiger");
PreparedStatement ps = conn.prepareStatement("insert into y02 (id,name,photo) values (1,'1.jpg',empty_lob())");
ps.executeUpdate();
String sql = "select photo from y02 where id = 1 for update";
ps = conn.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
rs.next();
BLOB b1 = (BLOB)rs.getBlob("photo");
InputStream is = new FileInputStream("1.jpg");
System.out.println(is.available());
OutputStream os = b1.getBinaryOutputStream();
byte[] b2 = new byte[1024*100];
int len = -1;
while ((len = is.read(b2)) != -1)
{
os.write(b2,0,len);
os.flush();
}
ps = conn.prepareStatement("update y02 set photo = ?");
ps.setBlob(1,b1);
ps.executeUpdate();
System.out.println(is.available());
conn.close();
}
};
/*
把读取存入类型为BLOB大数据类型的表中!
*/
package com.test;
import java.sql.*;
import oracle.sql.*;
import java.io.*;
public class H5
{
public static void main(String[] args) throws Exception
{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@192.168.6.6:1521:orcl","scott","tiger");
String sql = "select photo from y02 where id = 1";
PreparedStatement ps = conn.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
rs.next();
BLOB b1 = (BLOB)rs.getBlob("photo");
InputStream is = b1.getBinaryStream();
FileOutputStream fos = new FileOutputStream("20.jpg");
byte[] b2 = new byte[1024*100];
int len = -1;
while ((len = is.read(b2)) != -1)
{
fos.write(b2,0,len);
}
conn.close();
}
};