- public static void main(String[] args) throws Exception {
- // 读取BLOB数据
- Class.forName("oracle.jdbc.driver.OracleDriver");
- Connection con = DriverManager.getConnection(
- "jdbc:oracle:thin:@192.168.0.68:1521:myOracle", "my", "123");
- con.setAutoCommit(false);
- Statement st = con.createStatement();
- // 这里的SQL语句不再需要”for update”
- ResultSet rs = st.executeQuery(
- "select content from user_content where id = 2");
- if (rs.next()) {
- java.sql.Blob blob = rs.getBlob(1);
- InputStream ins = blob.getBinaryStream();
- // 用文件模拟输出流
- File file = new File("d:\\output.txt");
- OutputStream fout = new FileOutputStream(file);
- // 下面将BLOB数据写入文件
- byte[] b = new byte[1024];
- int len = 0;
- while ((len = ins.read(b)) != -1) {
- fout.write(b, 0, len);
- }
- // 依次关闭
- fout.close();
- ins.close();
- con.commit();
- con.close();
- }
- }
- }