六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 65|回复: 0

Clob存中文

[复制链接]

升级  8.67%

17

主题

17

主题

17

主题

秀才

Rank: 2

积分
63
 楼主| 发表于 2013-2-4 23:43:42 | 显示全部楼层 |阅读模式
数据文件直接使用中文
package core;
import java.io.*;
import java.sql.*;
import common.*;

/**
* @author Administrator
*
* TODO To change the template for this generated type comment go to Window -
* Preferences - Java - Code Style - Code Templates
*/
public class ClobEx {
  public static void addLob(int id, String binFile) throws SQLException {
    Connection con = null;
    PreparedStatement ps = null;
    ResultSet rs = null;

    try {
      con = ConnectionFactory.getFactory().getConnection();
      con.setAutoCommit(false);

      String sql = "INSERT INTO tbl_clob(id, remark) " +
          "VALUES(?, ?)";
      ps = con.prepareStatement(sql);      
      ps.setInt(1, id);
      ps.setClob(2, oracle.sql.CLOB.empty_lob());

      ps.executeUpdate();
      DBUtils.close(ps);
      ps = con.prepareStatement(
       "SELECT remark FROM tbl_clob" +
          " WHERE id = "  + id);
      
      rs = ps.executeQuery();    
      if (rs.next()) {      
        oracle.sql.CLOB remark =
         (oracle.sql.CLOB)rs.getClob(1);
        
        PrintWriter out = new PrintWriter(new OutputStreamWriter(remark.getAsciiOutputStream()));
        BufferedReader in = new BufferedReader(
           new FileReader(binFile));
        String s;
        while ((s = in.readLine()) != null) {
          out.println(s);              
        }        
        in.close();
        out.close();
      }
      con.commit();
    } catch (Exception e) {
      e.printStackTrace();
      try {
        con.rollback();
      } catch (SQLException se) {
      }
      throw new SQLException(e.getMessage());
    } finally {
      DBUtils.close(rs, ps, con);
    }
  }

  public static void fetchLob(int id, String filename) throws SQLException {
    Connection con = null;
    Statement st = null;
    ResultSet rs = null;

    try {
      con = ConnectionFactory.getFactory().getConnection();

      String sql = "SELECT * from tbl_clob where id = " + id;
      st = con.createStatement();

      rs = st.executeQuery(sql);
      if (rs.next()) {
        oracle.sql.CLOB remark =
          (oracle.sql.CLOB)rs.getClob("remark");

        PrintWriter out = new PrintWriter(
          new FileWriter(filename));            
        BufferedReader in = new BufferedReader(
         new InputStreamReader(
         remark.getAsciiStream(), "GB2312"));
        String s;
        while ((s = in.readLine()) != null) {
          out.println(s);          
        }
        in.close();
        out.close();
      }
    } catch (Exception e) {
      throw new SQLException(e.getMessage());
    } finally {
      DBUtils.close(rs, st, con);
    }
  }

  public static void main(String[] args) throws Exception {
    addLob(5, "test_u.txt");
    fetchLob(5, "test_c.txt");
    System.out.println("completed!");
  }
}
<span style="color: #003300;" />
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表