第四步,新建com.test.InsertClobData.java,用于插入clob数据类型的,代码如下:
- class InsertClobData
- {
- private ResultSet rs = null;
- private InitDB idb = null;
- InsertClobData()
- {
- idb = new InitDB();
- }
- public void insertClob(String sql1) throws SQLException
- {
- Connection con = idb.getCon();
- try
- {
- con.setAutoCommit(false);// 不设置自动提交
- BLOB blob = null; // 插入空的Clob
- PreparedStatement pstmt = con
- .prepareStatement("insert into cdl_test(sid,doc) values(?,empty_clob())");
- pstmt.setString(1, "101");
- pstmt.executeUpdate();
- pstmt.close();
- rs = con.createStatement().executeQuery(sql1);
- while (rs.next())
- {
- System.out.println("sdfasdfas");
- Oracle.sql.CLOB cb = (oracle.sql.CLOB) rs.getClob("doc");
- File f = new File("d:\\1.txt"); //1.txt一本小说《风云》马荣成
- System.out.println("file path is:" + f.getAbsolutePath());
- BufferedWriter out = new BufferedWriter(cb
- .getCharacterOutputStream());
- BufferedReader in = new BufferedReader(new FileReader(f));
- int c;
- while ((c = in.read()) != -1)
- {
- out.write(c);
- }
- in.close();
- out.close();
- }
- con.commit();
- }
- catch (Exception e)
- {
- con.rollback();// 出错回滚
- e.printStackTrace();
- }
- }
- }
