度量快速开发平台-专业、快速的软件定制快开平台

标题: java oracle clob string 大字符串存储【转】 [打印本页]

作者: caixuqad    时间: 2020-6-3 16:53

作者: fteair    时间: 2020-6-4 14:11
caixuqad 发表于 2017-8-3 16:53


作者: caixuqad    时间: 2020-6-5 19:13

作者: fteair    时间: 2020-6-7 14:34
caixuqad 发表于 2017-8-5 19:13


作者: caixuqad    时间: 2020-6-7 19:13

作者: fteair    时间: 2020-6-8 14:09
caixuqad 发表于 2017-8-7 19:13


作者: 张兴康    时间: 2020-6-8 14:41

作者: caixuqad    时间: 2020-6-8 17:52

作者: fteair    时间: 2020-6-24 14:36
标题: java oracle clob string 大字符串存储【转】
  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 = ? 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.         }
复制代码



作者: fteair    时间: 2020-6-24 14:36

作者: 张兴康    时间: 2020-6-24 17:21

作者: caixuqad    时间: 2020-6-24 17:58

作者: caixuqad    时间: 2020-6-24 17:59

作者: fteair    时间: 2020-6-25 14:49
张兴康 发表于 2017-7-24 17:21


作者: fteair    时间: 2020-6-25 14:51
caixuqad 发表于 2017-7-24 17:58


作者: fteair    时间: 2020-6-25 14:52
caixuqad 发表于 2017-7-24 17:59


作者: 张兴康    时间: 2020-6-25 17:54
fteair 发表于 2017-7-25 14:49


作者: caixuqad    时间: 2020-6-25 18:28

作者: caixuqad    时间: 2020-6-27 18:11

作者: fteair    时间: 2020-6-28 14:27
caixuqad 发表于 2017-7-27 18:11


作者: caixuqad    时间: 2020-6-30 19:16





欢迎光临 度量快速开发平台-专业、快速的软件定制快开平台 (http://bbs.delit.cn/) Powered by Discuz! X3.2