java 操作clob大字段
特别长的字符串想存到数据库中,的clob大字段中,大概4000字时报错,字符串太长,原因是sql语句大小不能超过4K,所以报这错,下面是我在网上找到的解决方法
package com.sinocec.model; import java.io.BufferedReader; import java.io.IOException; import java.io.Writer; import java.sql.Clob; 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 ClobTest { private static Connection conn; static { try { Class.forName("oracle.jdbc.driver.OracleDriver"); conn = DriverManager.getConnection( "jdbc:oracle:thin:@192.168.1.11:1521:orcl","scott","tiger"); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } } public static void main(String[] args) throws SQLException, IOException { //testInsert(); //testUpdate(); //testRead(); } private static void testInsert() throws SQLException { String sql = "insert into TECDESIGNALBUM (td_id,FILENAME,note) values (350,'bb',empty_clob())"; Statement stm = conn.createStatement(); stm.execute(sql); stm.close(); } private static void testUpdate() throws SQLException, IOException { String bigdata = "这是一个大字段。。。"; String sql = "select note from TECDESIGNALBUM where td_id = 350 for update"; Statement stm = conn.createStatement(); ResultSet rs = stm.executeQuery(sql); while (rs.next()) { Clob c = rs.getClob(1); c.truncate(0);// clear Writer w = c.setCharacterStream(1L);//The first position is 1 w.write(bigdata); w.close(); c.setString(c.length(), bigdata); conn.commit(); } conn.close(); } private static void testRead() throws SQLException, IOException { String sql = "select content from test_clob where id = 1"; PreparedStatement pstm = conn.prepareStatement(sql); ResultSet rs = pstm.executeQuery(); while (rs.next()) { Clob clob = rs.getClob("content"); System.out.println("clob.getSubString(1, 2) --> " + clob.getSubString(1, 2)); System.out.println("clob.getSubString(1, (int)clob.length()) --> " + clob.getSubString(1, (int)clob.length())); BufferedReader r = new BufferedReader(clob.getCharacterStream()); String s; while ((s = r.readLine()) != null) { System.out.println(s); } r.close(); } } }
相关推荐
Lzs 2020-10-23
聚合室 2020-11-16
零 2020-09-18
Justhavefun 2020-10-22
jacktangj 2020-10-14
ChaITSimpleLove 2020-10-06
Andrea0 2020-09-18
周游列国之仕子 2020-09-15
afanti 2020-09-16
88234852 2020-09-15
YClimb 2020-09-15
风雨断肠人 2020-09-04
卖口粥湛蓝的天空 2020-09-15
stulen 2020-09-15
pythonxuexi 2020-09-06
abfdada 2020-08-26
梦的天空 2020-08-25