java操作oracle 的clob字段

来源:互联网 发布:mac开机密码忘记了 编辑:程序博客网 时间:2024/05/21 10:11

import java.io.CharArrayReader;import java.io.CharArrayWriter;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.IOException;import java.io.InputStream;import java.io.Reader;import java.io.Writer;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;public class InsertClob {public static void main(String[] args) {try {Class.forName("oracle.jdbc.driver.OracleDriver");Connection con = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:ORCL", "scott", "tiger");   PreparedStatement pstmt = null; ResultSet rs = null; String query = "insert into testclob(ID,CONTENT) values(?,empty_clob())"; con.setAutoCommit(false); pstmt = con.prepareStatement(query);pstmt.setString(1,"6");pstmt.executeUpdate();pstmt = null;query = "select CONTENT from testclob where id = '6' for update";pstmt = con.prepareStatement(query);rs= pstmt.executeQuery();oracle.sql.CLOB clobtt = null;if(rs.next()){    clobtt = (oracle.sql.CLOB)rs.getClob(1);}Writer wr = clobtt.getCharacterOutputStream();InputStream is =new FileInputStream("d://1234.txt");int t =0;while((t=is.read())!= -1 ){wr.write(t);}wr.flush();wr.close();rs.close();con.commit();con.close();} catch (ClassNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();}catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}


 在oracle中查看clob字段的长度

select dbms_lob.getlength(content) from testclob where id =5


 

 

原创粉丝点击