第六步,新建com.test.ReadClobData.java,用于读取clob类型的数据,代码如下:
- class ReadClobData
- {
- private ResultSet rs = null;
- private InitDB idb = null;
- ReadClobData()
- {
- idb = new InitDB();
- }
- public void getClob(String sql2) throws SQLException
- {
- Connection con = idb.getCon();
- try
- {
- con.setAutoCommit(false);// 不设置自动提交
- System.out.println("sq2 is:" + sql2);
- rs = con.createStatement().executeQuery(sql2);
- while (rs.next())
- {
- Oracle.sql.CLOB clob = (oracle.sql.CLOB) rs.getClob("doc");
- File f = new File("d:\\1.txt");
- BufferedReader in = new BufferedReader(clob
- .getCharacterStream());
- // setCharacterStream()方法,可用于将CLOB字段与字节流相关联,
- BufferedWriter out = new BufferedWriter(new FileWriter(f));
- int c;
- while ((c = in.read()) != -1)
- {
- out.write(c);
- }
- out.close();
- in.close();
- }
- con.commit();// 正式提交
- rs.close();
- }
- catch (Exception e)
- {
- e.printStackTrace();
- con.rollback();
- }
- }
- }
