import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.Reader; import java.io.Writer; import java.sql.Clob; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; //存储大文本字符 public class ClobTest { /** * @param args * @throws IOException * @throws SQLException */ public static void main(String[] args) throws SQLException, IOException { create();//往数据库中存取大文本 read(); //从数据库中取出大文本 read_String(); test();//将一个文本中的内容读取到另一人文本中去 } static void create() throws SQLException, IOException { Connection conn = null; PreparedStatement pstmt = null; ResultSet rs = null; try { String sql = "insert into clob_test(big_test) values (?)"; // conn = jdbcUtilsSing.getInstance().getConnection(); pstmt = conn.prepareStatement(sql); // pstmt.setAsciiStream(parameterIndex, x, // length)若全是ascii码则可能采用ascii字符码 // 如果有其它非ascii字符则可以用别的方法的字符法,ps.setCharacterStream(); File file = new File("src/jdbcUtils.java"); Reader reader = new BufferedReader(new FileReader(file)); pstmt.setCharacterStream(1, reader, (int) file.length()); // reader.close(); int i = pstmt.executeUpdate(); // System.out.println("i=" + i); } finally { jdbcUtilsSing.getInstance().free(conn, pstmt, rs); } } static void read() throws SQLException, IOException { Connection conn = null; PreparedStatement pstmt = null; ResultSet rs = null; try { String sql = "select big_test from clob_test"; // conn = jdbcUtilsSing.getInstance().getConnection(); pstmt = conn.prepareStatement(sql); rs = pstmt.executeQuery(); while (rs.next()) { Clob clob = rs.getClob("big_test"); Reader reader = clob.getCharacterStream(); // reader = rs.getCharacterStream(1);//直接可以拿出一个字符流 File file = new File("jdbcUtilsSing_bak.java"); Writer writer = new BufferedWriter(new FileWriter(file)); char[] cbuf = new char[1024]; int i = 0; while ((i = reader.read(cbuf, 0, 1023)) != -1) { writer.write(cbuf, 0, i); } // 关闭 writer.close(); reader.close(); } } finally { jdbcUtilsSing.getInstance().free(conn, pstmt, rs); } } /** * 将数据库的大文本以String形式取出来 * @throws SQLException * @throws IOException */ static void read_String() throws SQLException, IOException { Connection conn = null; PreparedStatement pstmt = null; ResultSet rs = null; try { String sql = "select big_test from clob_test"; // conn = jdbcUtilsSing.getInstance().getConnection(); pstmt = conn.prepareStatement(sql); rs = pstmt.executeQuery(); while (rs.next()) { String string = rs.getString("big_test"); File file = new File("jdbcUtilsSing_bak.java"); Writer writer = new BufferedWriter(new FileWriter(file)); writer.write(string); writer.close(); } } finally { jdbcUtilsSing.getInstance().free(conn, pstmt, rs); } } /** * 流的读取操作,把一个文件中的内容读到另一个文件中去 * @throws SQLException * @throws IOException */ static void test() throws SQLException, IOException { File file = new File("src/jdbcUtils.java"); Reader reader = new BufferedReader(new FileReader(file)); String str = ""; char[] cbuf = new char[1024]; File file1 = new File("1.java"); Writer writer = new BufferedWriter(new FileWriter(file1)); // int i; while( (i=reader.read(cbuf,0,1023) )!=-1) { writer.write(cbuf, 0, i); } writer.flush(); } }