首页 > 代码库 > java oracle clob string 大字符串存储【转】
java oracle clob string 大字符串存储【转】
单位用到了oracle存储string类型到数据库里的clob,上网查看资料找到解决方案。如下:
1 public class ClobTest { 2 3 static String url = "jdbc:oracle:thin:@10.12.10.18:1521:orcl"; 4 static String user = "cwbe1_9999"; 5 static String pwd = "or777"; 6 static String text = "这是要插入到CLOB里面的数据,更新数据!" + "update"; 7 8 private static int clobImport() throws ClassNotFoundException, SQLException { 9 DriverManager.registerDriver(new OracleDriver()); 10 Connection conn = DriverManager.getConnection(url, user, pwd);// 得到连接对象 11 String sql = "insert into ETLNEEDPARAM(F_KEY,F_VALUE) values (‘defaultpo‘,?)";// 要执行的SQL语句 12 PreparedStatement stmt = conn.prepareStatement(sql);// 加载SQL语句 13 // PreparedStatement支持SQL带有问号?,可以动态替换?的内容。 14 Reader clobReader = new StringReader(text); // 将 text转成流形式 15 stmt.setCharacterStream(1, clobReader, text.length());// 替换sql语句中的? 16 int num = stmt.executeUpdate();// 执行SQL 17 if (num > 0) { 18 System.out.println("ok"); 19 } else { 20 System.out.println("NO"); 21 } 22 stmt.close(); 23 conn.close(); 24 return num; 25 } 26 27 private static int clobUpdate(String key) throws ClassNotFoundException, SQLException { 28 29 DriverManager.registerDriver(new OracleDriver()); 30 Connection conn = DriverManager.getConnection(url, user, pwd);// 得到连接对象 31 String sql = "update ETLNEEDPARAM set F_VALUE = http://www.mamicode.com/? where F_KEY = ?";// 要执行的SQL语句 32 PreparedStatement stmt = conn.prepareStatement(sql);// 加载SQL语句 33 // PreparedStatement支持SQL带有问号?,可以动态替换?的内容。 34 Reader clobReader = new StringReader(text); // 将 text转成流形式 35 stmt.setString(2, key); 36 stmt.setCharacterStream(1, clobReader, text.length());// 替换sql语句中的? 37 int num = stmt.executeUpdate();// 执行SQL 38 if (num > 0) { 39 System.out.println("ok"); 40 } else { 41 System.out.println("NO"); 42 } 43 stmt.close(); 44 conn.close(); 45 return num; 46 } 47 48 private static String clobExport() throws ClassNotFoundException, SQLException, IOException { 49 50 CLOB clob = null; 51 String sql = "select F_VALUE from ETLNEEDPARAM where F_KEY =‘test1‘"; 52 DriverManager.registerDriver(new OracleDriver()); 53 Connection conn = DriverManager.getConnection(url, user, pwd);// 得到连接对象 54 PreparedStatement stmt = conn.prepareStatement(sql); 55 ResultSet rs = stmt.executeQuery(); 56 String content = ""; 57 if (rs.next()) { 58 clob = (oracle.sql.CLOB) rs.getClob("F_VALUE"); // 获得CLOB字段str 59 // 注释: 用 rs.getString("str")无法得到 数据 ,返回的 是 NULL; 60 content = ClobToString(clob); 61 } 62 stmt.close(); 63 conn.close(); 64 return content; 65 } 66 67 // 将字CLOB转成STRING类型 68 69 public static String ClobToString(CLOB clob) throws SQLException, IOException { 70 String reString = ""; 71 Reader is = clob.getCharacterStream();// 得到流 72 BufferedReader br = new BufferedReader(is); 73 String s = br.readLine(); 74 StringBuffer sb = new StringBuffer(); 75 // 执行循环将字符串全部取出付值给StringBuffer由StringBuffer转成STRING 76 while (s != null) { 77 sb.append(s); 78 s = br.readLine(); 79 } 80 reString = sb.toString(); 81 return reString; 82 } 83 84 public static void main(String[] args) throws IOException,ClassNotFoundException, SQLException { 85 // System.out.println(clobImport()); 86 System.out.println(clobUpdate("fmo")); 87 System.out.println(clobUpdate("epo")); 88 89 // System.out.println(clobExport()); 90 91 }
转自:http://bestxiaok.iteye.com/blog/1027733
java oracle clob string 大字符串存储【转】
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。