首页

基于c3p0-oracle-thin-extras包中oracle工具处理类OracleUtils获取BLOB、CLOB大字段对象内容

标签:c3p0-oracle-thin-extras,OracleUtils,数据库工具类,blob,clob,大字段类型     发布时间:2018-01-03   

一、前言

基于c3p0-oracle-thin-extras包中com.mchange.v2.c3p0.dbms.OracleUtils数据库工具类实现获取oracle二进制大字段BLOB、文本文字大字段CLOB对象,并对其统一转换处理等。

二、源码说明

1.OracleUtils类

package com.mchange.v2.c3p0.dbms;@b@@b@import com.mchange.v2.c3p0.C3P0ProxyConnection;@b@import com.mchange.v2.sql.SqlUtils;@b@import java.lang.reflect.InvocationTargetException;@b@import java.lang.reflect.Method;@b@import java.sql.Connection;@b@import java.sql.SQLException;@b@import oracle.jdbc.driver.OracleConnection;@b@import oracle.sql.BLOB;@b@import oracle.sql.CLOB;@b@@b@public final class OracleUtils@b@{@b@  static final Class[] CREATE_TEMP_ARGS = { Connection.class, Boolean.TYPE, Integer.TYPE };@b@@b@  public static BLOB createTemporaryBLOB(Connection c3p0ProxyCon, boolean cache, int duration)@b@    throws SQLException@b@  {@b@    if (c3p0ProxyCon instanceof C3P0ProxyConnection)@b@    {@b@      try@b@      {@b@        C3P0ProxyConnection castCon = (C3P0ProxyConnection)c3p0ProxyCon;@b@        Method m = BLOB.class.getMethod("createTemporary", CREATE_TEMP_ARGS);@b@        Object[] args = { C3P0ProxyConnection.RAW_CONNECTION, Boolean.valueOf(cache), new Integer(duration) };@b@        return ((BLOB)castCon.rawConnectionOperation(m, null, args));@b@      }@b@      catch (InvocationTargetException e)@b@      {@b@        e.printStackTrace();@b@        throw SqlUtils.toSQLException(e.getTargetException());@b@      }@b@      catch (Exception e)@b@      {@b@        e.printStackTrace();@b@        throw SqlUtils.toSQLException(e);@b@      }@b@    }@b@    if (c3p0ProxyCon instanceof OracleConnection)@b@      return BLOB.createTemporary(c3p0ProxyCon, cache, duration);@b@@b@    throw new SQLException("Cannot create an oracle BLOB from a Connection that is neither an oracle.jdbc.driver.Connection, nor a C3P0ProxyConnection wrapped around an oracle.jdbc.driver.Connection.");@b@  }@b@@b@  public static CLOB createTemporaryCLOB(Connection c3p0ProxyCon, boolean cache, int duration)@b@    throws SQLException@b@  {@b@    if (c3p0ProxyCon instanceof C3P0ProxyConnection)@b@    {@b@      try@b@      {@b@        C3P0ProxyConnection castCon = (C3P0ProxyConnection)c3p0ProxyCon;@b@        Method m = CLOB.class.getMethod("createTemporary", CREATE_TEMP_ARGS);@b@        Object[] args = { C3P0ProxyConnection.RAW_CONNECTION, Boolean.valueOf(cache), new Integer(duration) };@b@        return ((CLOB)castCon.rawConnectionOperation(m, null, args));@b@      }@b@      catch (InvocationTargetException e)@b@      {@b@        e.printStackTrace();@b@        throw SqlUtils.toSQLException(e.getTargetException());@b@      }@b@      catch (Exception e)@b@      {@b@        e.printStackTrace();@b@        throw SqlUtils.toSQLException(e);@b@      }@b@    }@b@    if (c3p0ProxyCon instanceof OracleConnection)@b@      return CLOB.createTemporary(c3p0ProxyCon, cache, duration);@b@@b@    throw new SQLException("Cannot create an oracle CLOB from a Connection that is neither an oracle.jdbc.driver.Connection, nor a C3P0ProxyConnection wrapped around an oracle.jdbc.driver.Connection.");@b@  }@b@}

2.SqlUtils类

package com.mchange.v2.sql;@b@@b@import com.mchange.lang.ThrowableUtils;@b@import java.sql.SQLException;@b@import java.text.DateFormat;@b@import java.text.SimpleDateFormat;@b@import java.util.Date;@b@@b@public final class SqlUtils@b@{@b@  static final DateFormat tsdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSS");@b@  public static final String DRIVER_MANAGER_USER_PROPERTY = "user";@b@  public static final String DRIVER_MANAGER_PASSWORD_PROPERTY = "password";@b@@b@  public static String escapeBadSqlPatternChars(String s)@b@  {@b@    StringBuffer sb = new StringBuffer(s);@b@    int i = 0; for (int len = sb.length(); i < len; ++i)@b@      if (sb.charAt(i) == '\'')@b@      {@b@        sb.insert(i, '\'');@b@        ++len;@b@        i += 2;@b@      }@b@    return sb.toString();@b@  }@b@@b@  public static String escapeAsTimestamp(Date date) {@b@    return "{ts '" + tsdf.format(date) + "'}";@b@  }@b@@b@  public static SQLException toSQLException(Throwable t) {@b@    if (t instanceof SQLException) {@b@      return ((SQLException)t);@b@    }@b@@b@    t.printStackTrace();@b@    return new SQLException(ThrowableUtils.extractStackTrace(t));@b@  }@b@}