Oracle里读取Blob/Clob 乱码的解决办法

 
  1. package com.logcd.common;   
  2.   
  3. import java.io.BufferedReader;   
  4. import java.io.File;   
  5. import java.io.FileInputStream;   
  6. import java.io.InputStreamReader;   
  7. import java.io.Reader;   
  8. import java.io.Writer;   
  9. import java.net.HttpURLConnection;   
  10. import java.net.URL;   
  11. import java.sql.Connection;   
  12. import java.sql.DriverManager;   
  13. import java.sql.ResultSet;   
  14. import java.sql.SQLException;   
  15. import java.sql.Statement;   
  16.   
  17. import org.apache.commons.io.FileUtils;   
  18.   
  19. import oracle.sql.CLOB;   
  20.   
  21. public class OracleClob {   
  22.   
  23.     public static void main(String[] args) {   
  24.   
  25.         Integer id = saveClobDataUseSQL("测试", getFileContentAsString(   
  26.                 "D:/uploadDir/test.txt"true));   
  27.   
  28.         readClobDataUseSQL(id, new File("D:/uploadDir/test2.txt"));   
  29.     }   
  30.   
  31.     /**  
  32.      * 保存Clob数据  
  33.      *   
  34.      * @param name  
  35.      *            数据名称  
  36.      * @param data  
  37.      *            字串数据  
  38.      */  
  39.     @SuppressWarnings("deprecation")   
  40.     public static Integer saveClobDataUseSQL(String name, String data) {   
  41.         Connection conn = getConnection();   
  42.         Integer id = (int) (Math.random() * 100000);   
  43.   
  44.         StringBuilder sqlBuilder = new StringBuilder();   
  45.         try {   
  46.             conn.setAutoCommit(false);   
  47.             Statement stmt = conn.createStatement();   
  48.   
  49.             sqlBuilder.append("insert into TEST_CLOB(ID, NAME, CONTENT) ");   
  50.             sqlBuilder.append("values ( " + id);   
  51.             sqlBuilder.append(",'" + name + "'");   
  52.             sqlBuilder.append(", empty_clob()) ");// 插入一个空对象empty_clob()   
  53.   
  54.             stmt.executeUpdate(sqlBuilder.toString());   
  55.             // 锁定数据行进行更新,注意“for update”语句   
  56.             String sqlUpd = "select CONTENT from TEST_CLOB where ID = " + id   
  57.                     + " for update";   
  58.             ResultSet rs = stmt.executeQuery(sqlUpd);   
  59.             if (rs.next()) {   
  60.                 // 得到java.sql.Clob对象后强制转换为oracle.sql.CLOB   
  61.                 CLOB clob = (CLOB) rs.getClob("CONTENT");   
  62.                 Writer outStream = clob.setCharacterStream(0L);   
  63.                 // data是传入的字符串,定义:String data   
  64.                 char[] c = data.toCharArray();   
  65.                 outStream.write(c, 0, c.length);   
  66.   
  67.                 outStream.flush();   
  68.                 outStream.close();   
  69.   
  70.                 conn.commit();   
  71.                 stmt.close();   
  72.             }   
  73.   
  74.         } catch (Exception e) {   
  75.             try {   
  76.                 conn.rollback();   
  77.                 id = null;   
  78.             } catch (SQLException e1) {   
  79.                 e1.printStackTrace();   
  80.             }   
  81.             e.printStackTrace();   
  82.         } finally {   
  83.             try {   
  84.                 conn.close();   
  85.             } catch (SQLException e) {   
  86.                 e.printStackTrace();   
  87.             }   
  88.         }   
  89.         return id;   
  90.     }   
  91.   
  92.     /**  
  93.      * 读出数据并存成文件  
  94.      *   
  95.      * @param id  
  96.      * @param file  
  97.      */  
  98.     public static void readClobDataUseSQL(Integer id, File file) {   
  99.         Connection conn = getConnection();   
  100.         try {   
  101.             Statement st = conn.createStatement();   
  102.             String sql = "select CONTENT from TEST_CLOB where ID = " + id;   
  103.             ResultSet rs = st.executeQuery(sql);   
  104.   
  105.             if (rs.next()) {   
  106.   
  107.                 CLOB clob = (CLOB) rs.getClob("CONTENT");   
  108.   
  109.                 String result = convertClobToString(clob);   
  110.                 System.out.println(result);   
  111.                 FileUtils.writeStringToFile(file,result, "utf-8");   
  112.             }   
  113.         } catch (Exception e) {   
  114.             e.printStackTrace();   
  115.         } finally {   
  116.             try {   
  117.                 conn.close();   
  118.             } catch (SQLException e) {   
  119.                 e.printStackTrace();   
  120.             }   
  121.         }   
  122.   
  123.     }   
  124.   
  125.     /**  
  126.      * 将CLOB转换成字串  
  127.      * @param clob  
  128.      * @return  
  129.      */  
  130.     public static String convertClobToString(CLOB clob) {   
  131.         String reString = "";   
  132.         try {   
  133.             Reader is = clob.getCharacterStream();// 得到流   
  134.             BufferedReader br = new BufferedReader(is);   
  135.             String s = br.readLine();   
  136.             StringBuffer sb = new StringBuffer();   
  137.             while (s != null) {   
  138.                 sb.append(s);   
  139.                 sb.append("/n");   
  140.                 s = br.readLine();   
  141.             }   
  142.             reString = sb.toString().trim();   
  143.         } catch(Exception e) {   
  144.             e.printStackTrace();   
  145.         }   
  146.         return reString;   
  147.     }   
  148.   
  149.     /**  
  150.      * 取得数据库连接  
  151.      *   
  152.      * @return  
  153.      */  
  154.     public static Connection getConnection() {   
  155.         String driver = "oracle.jdbc.driver.OracleDriver";   
  156.         String url = "jdbc:oracle:thin:@195.2.199.5:1521:orcl";   
  157.         Connection conn = null;   
  158.         try {   
  159.             Class.forName(driver);   
  160.             conn = DriverManager.getConnection(url, "testdb""logcd");   
  161.         } catch (ClassNotFoundException e) {   
  162.             e.printStackTrace();   
  163.         } catch (SQLException ex) {   
  164.             ex.printStackTrace();   
  165.         }   
  166.         return conn;   
  167.     }   
  168.   
  169.     /**  
  170.      * 读取文件内容  
  171.      *   
  172.      * @param _url  
  173.      * @param isLocal  
  174.      * @return  
  175.      */  
  176.     public static String getFileContentAsString(String _url, boolean isLocal) {   
  177.         StringBuilder strb = new StringBuilder();   
  178.         try {   
  179.             InputStreamReader read = null;   
  180.             ;   
  181.             if (!isLocal) {   
  182.                 URL url = new URL(_url);   
  183.                 HttpURLConnection connection = (HttpURLConnection) url   
  184.                         .openConnection();   
  185.                 read = new InputStreamReader(connection.getInputStream());   
  186.             } else {   
  187.                 File file = new File(_url);   
  188.                 read = new InputStreamReader(new FileInputStream(file));   
  189.             }   
  190.             BufferedReader br = new BufferedReader(read);   
  191.   
  192.             char[] cbuf = new char[1024];   
  193.             while (br.read(cbuf) != -1) {   
  194.                 strb.append(cbuf);   
  195.             }   
  196.         } catch (Exception e) {   
  197.             e.printStackTrace();   
  198.         }   
  199.   
  200.         return strb.toString();   
  201.     }   
  202.   
  203. }  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值