标签:localhost oracle public 数据库 tiger
public class TestBLOB {
static String url = "jdbc:oracle:thin:@localhost:1521:orcl";
static String driver = "oracle.jdbc.driver.OracleDriver";
static String userName = "scott";
static String pwd = "tiger";
public static void main(String[] args) {
try {
// addBLOB();
readBLOB();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
// public static void addBLOB() throws ClassNotFoundException, SQLException, IOException{
// Class.forName(driver); //加载数据库驱动
// Connection con = DriverManager.getConnection(url, userName, pwd);
// con.setAutoCommit(false); //关闭自动提交
// Statement stmt = con.createStatement();
// stmt.executeUpdate("UPDATE ATTACHMENT SET MYBLOB = EMPTY_BLOB() WHERE ID=1");
// String strSQL = "SELECT MYBLOB FROM ATTACHMENT WHERE id = 1 ";
// ResultSet rs = stmt.executeQuery(strSQL);
// BLOB blob = null;
// if(rs.next()){
// blob = (BLOB) rs.getBlob(1);
// }
// OutputStream os = blob.setBinaryStream(1);
// File file = new File("F:\\aaa.pdf");
// InputStream is = new FileInputStream(file);
// byte [] b = new byte[1024];
// int len = 0;
// while((len = is.read(b)) != -1){
// os.write(b,0,len);
// }
// os.close();
// is.close();
// con.commit();
// con.close();
// }
public static void readBLOB() throws ClassNotFoundException, SQLException, IOException{
Class.forName(driver); //加载数据库驱动
Connection con = DriverManager.getConnection(url,userName,pwd);
con.setAutoCommit(false); //关闭自动提交
PreparedStatement pstm = con.prepareCall("select myblob from attachment where id=1");
ResultSet rs = pstm.executeQuery();
BLOB blob = null;
if(rs.next()){
blob = (BLOB) rs.getBlob(1);
}
InputStream is = blob.getBinaryStream();
byte [] b = new byte[1024];
int len = 0;
File file = new File("D:\\bbb.pdf");
OutputStream os = new FileOutputStream(file);
while((len = is.read(b))!= -1){
os.write(b,0,len); //0表示起始位置,len表示长度
}
is.close();
os.close();
con.commit();
con.close();
}
}
标签:localhost oracle public 数据库 tiger
原文地址:http://9882931.blog.51cto.com/9872931/1615017