您现在的位置是:首页 >

clob字段如何比较 jdbc方式读取oracle的clob字段实例

火烧 2021-06-24 00:10:56 1061
jd c方式读取oracle的clo 字段实例 可能大家也都习惯了 ri g和hi er ate对CLOB字段的处理 在 ri g中配置clo 的oracle处理句柄 在hi er ate中配置映射类

jdbc方式读取oracle的clob字段实例  

          可能大家也都习惯了spring和hibernate对CLOB字段的处理 在spring中配置clob的oracle处理句柄 在hibernate中配置映射类型 然后就可以很轻松的以String 的形式处理大字段          今天我做了个需求 需要以jdbc的方式从mysql导一些备份数据到oracle正式库 就查了一些资料 最后写了个例子 首先 写个连接数据库的类 里面有返回mysq oracle连接的方法

  public Connection getConn(String flag){

  Connection con=null;

  try

  {

  if(flag equals( ))

  {

  Class forName( oracle jdbc driver OracleDriver );

  con = DriverManager getConnection( jdbc:oracle:thin:@IP: :数据库名字 name password );

  }

  if(flag equals( ))

  {

  Class forName( gjt mm mysql Driver );

  con = DriverManager getConnection( jdbc:mysql://localhost/数据库名?user=用户名&password=密码&useUnicode=true&characterEncoding=GBK );

  }

  }

  catch(Exception e)

  {

  e printStackTrace();

  }

  return con;

  }

  public void setData() {

  conn = new Conn();

  try {

  String sqlfrom = select  p id ntent from <A title=table target=_blank>table</A> p  order by p id   ;

  String sqlinsert = insert into <A title=table target=_blank>table</A>   values(? ?) ;

  con = conn getConn( );

  stmt = con createStatement(); //从mysql取出大字段

  rs = stmt executeQuery(sqlfrom);

  con = conn getConn( );

  PreparedStatement pstmt = con prepareStatement(sqlinsert); //向oracle中插入大字段

  int i = ;

  while (rs next()) {

  pstmt setInt( rs getInt( ));

  pstmt setClob( <A title=oracle 股票&amp;amp;q=股票&amp;amp;sbb=搜索&amp;amp;sa=搜索&amp;amp;client=pub &amp;amp;forid= &amp;amp;prog=aff&amp;amp;ie=GB &amp;amp;oe=GB &amp;amp;hl=zh CN target=_blank>oracle</A> sql CLOB empty_lob());

  pstmt executeUpdate();  //插入时将大字段设为空

  this updateOne(con rs getInt( ) rs getString( ));  // 这里调用然后更新这个大字段

  }

  rs close();  //关闭相关连接

  pstmt close();

  stmt close();

  con close();

  } catch (Exception e) {

  e printStackTrace();

  try

  {

  con rollback();

  } catch (Exception e ) {

  <A title=system target=_blank>system</A> out println( 回滚出现异常! );

  e printStackTrace();

  }

  }

  }    :该方法实现对应大字段记录的更新

  <PRE class=java name= code >public void updateOne(Connection con int id String content) {

  String str = select ntent from <A title=table target=_blank>table</A>  t where t id= + id+   for update ;

  try {

  // 注意 存取操作开始前 必须用setAutoCommit(false)取消自动提交 否则Oracle将抛出 读取违反顺序 的错误

  con setAutoCommit(false);

  stmt = con createStatement();

  ResultSet   rs_clob = stmt executeQuery(str);

  while ( rs_clob next()) {

  

  <A title=oracle 股票&amp;amp;q=股票&amp;amp;sbb=搜索&amp;amp;sa=搜索&amp;amp;client=pub &amp;amp;forid= &amp;amp;prog=aff&amp;amp;ie=GB &amp;amp;oe=GB &amp;amp;hl=zh CN target=_blank>oracle</A> sql CLOB clob = (oracle sql CLOB)  rs_clob getClob( );

  

  clob putString( content);

  }

  stmt close();

  mit();

  con setAutoCommit(true);

  con close();

  } catch (Exception e) {

  e printStackTrace();

  try

  {

clob字段如何比较 jdbc方式读取oracle的clob字段实例

  con rollback();

  } catch (Exception e ) {

  <A title=system target=_blank>system</A> out println( 回滚出现异常! );

  e printStackTrace();

  }

  }

  }

  </PRE>                        现在就完成了一行记录的更新

   读clob字段以String 的形式返回(当然也可以将读到的内容写入文件 大家改一下就可以了)

  <PRE class=java name= code >/**

  * 读clob字段

  * @param con

  * @param id

  * @return

  */

  public String  readClob(Connection con int id)

  {

  String content= ;

  try

  {

  con setAutoCommit(false);

  stmt=con createStatement();

  ResultSet rs_clob=stmt executeQuery( select  ntent  from  <A title=table target=_blank>table</A> t where t id= +id);

  <A title=oracle 股票&amp;amp;q=股票&amp;amp;sbb=搜索&amp;amp;sa=搜索&amp;amp;client=pub &amp;amp;forid= &amp;amp;prog=aff&amp;amp;ie=GB &amp;amp;oe=GB &amp;amp;hl=zh CN target=_blank>oracle</A> sql CLOB contents=null;

  while (rs_clob next())

  {      // 取出CLOB对象

  contents= (oracle sql CLOB) rs_clob getClob( );

  }

  BufferedReader a = new BufferedReader(contents getCharacterStream());  //以字符流的方式读入BufferedReader

  String str = ;

  while ((str = a readLine()) != null) {

  content = ncat(str);  //最后以String的形式得到

  }

  mit();

  

  con setAutoCommit(true);

  con close();

  }catch(Exception e)

  {

  <A title=system target=_blank>system</A> out println( 出现异常 );

  e printStackTrace();

  try

  {

  con rollback();

  }

  catch (Exception e )

  {

  <A title=system target=_blank>system</A> out println( 回滚出现异常! );

  e printStackTrace();

  }

  }

  return content;

  }

  </PRE>

lishixinzhi/Article/program/Java/hx/201311/26226  
永远跟党走
  • 如果你觉得本站很棒,可以通过扫码支付打赏哦!

    • 微信收款码
    • 支付宝收款码