JDBC操作oracle中的大对象
1 一:操作CLOB 2 3 (1)数据库表结构如下: 4 5 6 create table CLOB_TEST 7 ( 8 ID VARCHAR2(5) not null, 9 CONTENT CLOB 10 ) 11 12 (2)插入CLOB 13 14 方法一:第一步插入一个空值,第二步锁住此行,更新clob字段 15 16 public static void insertClob(Connection conn,String data) throws Exception{ //这句话如没有,9i的驱动下会报 java.sql.SQLException: ORA-01002: 读取违反顺序 的异常。 conn.setAutoCommit(false); 17 //插入一个空CLOB String insertSql = "insert into clob_test(id,content) values('1',empty_clob())"; //查询插入的空CLOB String selectSql = "select content from clob_test where id = '1' for update"; PreparedStatement stmt = conn.prepareStatement(insertSql); stmt.executeUpdate(); stmt.close(); // lock this line PreparedStatement pstmt = conn.prepareStatement(selectSql); ResultSet rs = pstmt.executeQuery(); if(rs.next()){ oracle.sql.CLOB clob = (oracle.sql.CLOB)rs.getClob(1); //为CLOB写信息 BufferedWriter out = new BufferedWriter(clob.getCharacterOutputStream()); BufferedReader in = new BufferedReader(new FileReader(data)); int c; while ((c=in.read())!=-1) { out.write(c); } in.close(); out.close(); } conn.commit(); pstmt.close(); } 18 19 注:此方法在jdk1.4、jdk50、jdk6.0和Oracle9i、Oracle10g、Oracle11g驱动下测试通过! 20 21 方法二:通过setString方法 22 23 public static void insertClob(Connection conn,String data) throws Exception{ String insertSql = "insert into clob_test(id,content) values('1',?)"; PreparedStatement stmt = conn.prepareStatement(insertSql); stmt.setString(1, data); stmt.executeUpdate(); stmt.close(); conn.close(); 24 25 } 26 27 28 注:由于在Oracle9i的驱动下,setString 有2000字符长度的限制,故这个方法只适合Oracle10g以上的驱动(Oracle11g驱动+JDK6.0也测试通过)。 29 30 方法三:通过setClob方法 31 32 public static void insertClob(Connection conn,String filePath) throws Exception{ String insertSql = "insert into clob_test(id,content) values('1',?)"; PreparedStatement stmt = conn.prepareStatement(insertSql); stmt.setClob(1, new FileReader(filePath)); stmt.executeUpdate(); stmt.close(); conn.commit(); } 33 34 注:由于setClob(int parameterIndex, Reader reader)这个方法是JDBC4.0规范刚加的内容,是以流的方式为CLOB赋值的。并且Oracle9i驱动、Oracle10g驱动、JDK1.4、JDK1.5是基于JDBC3.0规范的,只有Oracle11g驱动+JDK6.0才是基于JDBC4.0规范的,所以目前这个方法只适合Oracle11g驱动(ojdbc6.jar)+JDK6.0! 35 36 (3)读取CLOB 37 38 方法一: 39 40 public static String readClob(Connection conn) throws Exception{ PreparedStatement stmt = conn.prepareStatement("select * from clob_test where id = '1'"); ResultSet rs = stmt.executeQuery(); String str=""; StringBuffer sb = new StringBuffer(""); while(rs.next()){ Clob clob = rs.getClob("content"); Reader is = clob.getCharacterStream(); BufferedReader br = new BufferedReader(is); str = br.readLine(); while (str != null) { sb.append(str); str = br.readLine(); } } return sb.toString(); } 41 42 方法二: 43 public static String readClob(Connection conn) throws Exception{ PreparedStatement stmt = conn.prepareStatement("select * from clob_test where id = '1'"); ResultSet rs = stmt.executeQuery(); String str=""; while(rs.next()){ str = rs.getString("content"); } return str; } 44 45 注:由于在Oracle9i的驱动下,rs.getString 返回为null,所以此方法只适合Oracle10g及其以上驱动。 46 47 二:操作BLOB 48 49 (1)数据库表结构如下: 50 51 52 create table BLOB_TEST 53 ( 54 ID VARCHAR2(5) not null, 55 CONTENT BLOB 56 ) 57 58 (2)插入BLOB 59 60 方法一:第一步插入一个空值,第二步锁住此行,更新blob字段 61 62 public static void writeBlob(Connection con,String filePath) throws Exception{ FileInputStream fis = null; PreparedStatement psm = null; File file = new File(filePath); psm = con.prepareStatement("insert into blob_test(id,content) values('2',empty_blob())"); psm.executeUpdate(); psm = con.prepareStatement("select content from blob_test where id ='2' for update"); ResultSet rs = psm.executeQuery(); if(rs.next()){ oracle.sql.BLOB blob = (oracle.sql.BLOB)rs.getBlob(1); FileInputStream fin = new FileInputStream(file); OutputStream out = blob.getBinaryOutputStream(); int count = -1, total = 0; byte[] data = new byte[blob.getBufferSize()]; while ((count = fin.read(data)) != -1) { out.write(data, 0, count); } out.flush(); out.close(); 63 } } 64 65 方法二:通过setBinaryStream方法 66 67 public static void writeBlob(Connection con,String filePath) throws Exception{ FileInputStream fis = null; PreparedStatement psm = null; File file = new File(filePath); try { fis = new FileInputStream(file); psm = con.prepareStatement("insert into blob_test(id,content) values('2',?)"); psm.setBinaryStream(1, fis, fis.available()); psm.executeUpdate(); }finally{ if(fis != null) fis.close(); psm.close(); con.close(); } } 68 69 方法三:通过setBlob(int parameterIndex, InputStream inputStream)方法 70 71 public static void writeBlob(Connection con,String filePath) throws Exception{ FileInputStream fis = null; PreparedStatement psm = null; File file = new File(filePath); try { fis = new FileInputStream(file); psm = con.prepareStatement("insert into blob_test(id,content) values('2',?)"); psm.setBlob(1, fis); psm.executeUpdate(); }finally{ if(fis != null) fis.close(); psm.close(); con.close(); } } 72 73 74 注:由于setBlob(int parameterIndex, InputStream inputStream)这个方法是JDBC4.0规范刚加的内容,是以流的方式为BLOB赋值的。并且Oracle9i驱动、Oracle10g驱动、JDK1.4、JDK1.5是基于JDBC3.0规范的,只有Oracle11g驱动+JDK6.0才是基于JDBC4.0规范的,所以目前这个方法只适合Oracle11g驱动(ojdbc6.jar)+JDK6.0! 75 76 (3)读取BLOB 77 78 public static void readBlob(Connection con,String outFilePath){ Statement sm = null; ResultSet rs = null; try { sm = con.createStatement(); rs = sm.executeQuery("select * from blob_test where id = 2"); if(rs.next()){ Blob blob = rs.getBlob("content"); File file = new File(outFilePath); FileOutputStream sout = new FileOutputStream(file); InputStream in = blob.getBinaryStream();//获取BLOB数据的输入数据流 //经BLOB输入数据流读取数据,并将其写入文件 byte[] b = new byte[256]; int off = 0; int len = b.length; for (int i = in.read(b); i != -1;) { sout.write(b); i = in.read(b); } sout.close(); rs.close(); sm.close(); con.close(); } } catch (Exception e) { e.printStackTrace(); } }