一、前言

基于dbcon-5.0.1.jar.zip开源包的数据库工具类uk.ac.sanger.cgp.dbcon.util.DatabaseUtils对数据连接有效关闭closeDbObject、数据库类型判断isOracle、数据库账号登录验证isUrlValid及获取验证后有效连接配置getValidConnectionURL等,具体见如下源码说明。

二、源码说明

1.DatabaseUtils工具类package uk.ac.sanger.cgp.dbcon.util;@b@@b@import java.sql.Connection;@b@import java.sql.DatabaseMetaData;@b@import java.sql.DriverManager;@b@import java.sql.ResultSet;@b@import java.sql.SQLException;@b@import java.sql.Statement;@b@import org.apache.commons.logging.Log;@b@import org.apache.commons.logging.LogFactory;@b@import uk.ac.sanger.cgp.dbcon.config.Config;@b@import uk.ac.sanger.cgp.dbcon.exceptions.DbConException;@b@@b@public class DatabaseUtils@b@{@b@  private static final Log LOG = LogFactory.getLog(DatabaseUtils.class);@b@@b@  public static void connectionSettings(Connection conn)@b@  {@b@    try@b@    {@b@      conn.setAutoCommit(false);@b@      conn.rollback();@b@    }@b@    catch (SQLException e) {@b@      throw new DbConException("Error whilst setting autoCommit off and rollbacking the connection", e);@b@    }@b@  }@b@@b@  public static void closeDbObject(Connection conn)@b@  {@b@    if (conn != null)@b@      try {@b@        conn.rollback();@b@      }@b@      catch (SQLException e) {@b@        LOG.warn("Could not rollback java.sql.Connection", e);@b@      }@b@      finally {@b@        try {@b@          conn.close();@b@        }@b@        catch (SQLException e) {@b@          LOG.warn("Could not close java.sql.Connection", e);@b@        }@b@      }@b@  }@b@@b@  public static void closeDbObject(Statement st)@b@  {@b@    if (st != null)@b@      try {@b@        st.close();@b@      }@b@      catch (SQLException e) {@b@        LOG.warn("Could not close java.sql.Statement", e);@b@      }@b@  }@b@@b@  public static void closeDbObject(ResultSet rs)@b@  {@b@    if (rs != null)@b@      try {@b@        rs.close();@b@      }@b@      catch (SQLException e) {@b@        LOG.warn("Could not close java.sql.ResultSet", e);@b@      }@b@  }@b@@b@  public static boolean isOracle(Connection conn)@b@    throws SQLException@b@  {@b@    return conn.getMetaData().getDriverName().matches("^Oracle.+");@b@  }@b@@b@  public static boolean isFixRequiredForOracleVersion(Connection conn)@b@    throws SQLException@b@  {@b@    int majorVersion = conn.getMetaData().getDriverMajorVersion();@b@    if (majorVersion > 10) throw new DbConException("Unknown Oracle driver major version: cannot determine whether to apply V8 compatibility");@b@    return (majorVersion == 10);@b@  }@b@@b@  public static boolean setOraclePropertyForBugFix(Connection conn)@b@    throws SQLException@b@  {@b@    if ((!(Boolean.getBoolean("oracle.jdbc.V8Compatible"))) && (isOracle(conn)) && (isFixRequiredForOracleVersion(conn)))@b@    {@b@      System.setProperty("oracle.jdbc.V8Compatible", "true");@b@      return true;@b@    }@b@@b@    return false;@b@  }@b@@b@  public static String getValidConnectionURL(Config config)@b@    throws DbConException@b@  {@b@    String validUrl = "";@b@@b@    if (isUrlValid(config.getUrl(), config.getUsername(), config.getPassword(), config.getDriver())) {@b@      validUrl = config.getUrl();@b@    }@b@    else if ((config.getBackupUrl() != null) && (isUrlValid(config.getBackupUrl(), config.getUsername(), config.getPassword(), config.getDriver()))) {@b@      validUrl = config.getBackupUrl();@b@    }@b@    else {@b@      throw new DbConException("No valid URL could be found for DB " + config.getName());@b@    }@b@@b@    config.setWorkingUrl(validUrl);@b@@b@    return validUrl;@b@  }@b@@b@  public static boolean isUrlValid(String url, String username, String password, String driver)@b@  {@b@    boolean okay = false;@b@    Connection conn = null;@b@    try {@b@      Class.forName(driver);@b@      conn = DriverManager.getConnection(url, username, password);@b@      okay = true;@b@    }@b@    catch (ClassNotFoundException e) {@b@      if (LOG.isDebugEnabled()) LOG.debug("Cannot find class", e);@b@      okay = false;@b@    }@b@    catch (SQLException e) {@b@      if (LOG.isDebugEnabled()) LOG.debug("SQLException issue", e);@b@      okay = false;@b@    }@b@    finally {@b@      closeDbObject(conn);@b@    }@b@@b@    return okay;@b@  }@b@}

2.Config数据连接配置类package uk.ac.sanger.cgp.dbcon.config;@b@@b@import java.io.Serializable;@b@@b@public class Config@b@  implements Serializable, Cloneable@b@{@b@  private static final long serialVersionUID = 8255104234840048031L;@b@  public static final byte FAIL = 0;@b@  public static final byte BLOCK = 1;@b@  public static final byte GROW = 2;@b@  private int maxActive = 0;@b@  private int maxIdle = 0;@b@  private int numTestsPerEvictionRun = 0;@b@  private byte exhausted = 0;@b@  private long maxWait = 0L;@b@  private long timeBetweenEvictRun = 0L;@b@  private long minEvictTime = 0L;@b@  private int cachedPreparedStatements = 0;@b@  private boolean testOnBorrow = false;@b@  private boolean testOnReturn = false;@b@  private boolean testWhileIdle = false;@b@  private String url = null;@b@  private String backupUrl = null;@b@  private String username = null;@b@  private String password = null;@b@  private String name = null;@b@  private String validationQuery = null;@b@  private String driver = null;@b@  private String workingUrl = null;@b@@b@  private Config()@b@  {@b@  }@b@@b@  public static Config createEmptyConfig()@b@  {@b@    return new Config();@b@  }@b@@b@  public static Config createBasicPoolConfig(int maxActive, byte exhausted, long maxWait, int maxIdle, boolean testOnBorrow, boolean testOnReturn, long timeBetweenEvictRun, int numTestsPerEvictionRun, long minEvictTime, boolean testWhileIdle, String url, String username, String password, String name, String validationQuery, String driver, int cachedPreparedStatements)@b@  {@b@    Config config = new Config(maxActive, exhausted, maxWait, maxIdle, testOnBorrow, testOnReturn, timeBetweenEvictRun, numTestsPerEvictionRun, minEvictTime, testWhileIdle, url, username, password, name, validationQuery, driver, null, cachedPreparedStatements);@b@@b@    return config;@b@  }@b@@b@  public static Config createBasicPoolConfigWithBackup(int maxActive, byte exhausted, long maxWait, int maxIdle, boolean testOnBorrow, boolean testOnReturn, long timeBetweenEvictRun, int numTestsPerEvictionRun, long minEvictTime, boolean testWhileIdle, String url, String username, String password, String name, String validationQuery, String driver, String backupUrl, int cachedPreparedStatements)@b@  {@b@    Config config = new Config(maxActive, exhausted, maxWait, maxIdle, testOnBorrow, testOnReturn, timeBetweenEvictRun, numTestsPerEvictionRun, minEvictTime, testWhileIdle, url, username, password, name, validationQuery, driver, backupUrl, cachedPreparedStatements);@b@@b@    return config;@b@  }@b@@b@  private Config(int maxActive, byte exhausted, long maxWait, int maxIdle, boolean testOnBorrow, boolean testOnReturn, long timeBetweenEvictRun, int numTestsPerEvictionRun, long minEvictTime, boolean testWhileIdle, String url, String username, String password, String name, String validationQuery, String driver, String backupUrl, int cachedPreparedStatements)@b@  {@b@    this.maxActive = maxActive;@b@    this.exhausted = exhausted;@b@    this.maxWait = maxWait;@b@    this.maxIdle = maxIdle;@b@    this.testOnBorrow = testOnBorrow;@b@    this.testOnReturn = testOnReturn;@b@    this.timeBetweenEvictRun = timeBetweenEvictRun;@b@    this.numTestsPerEvictionRun = numTestsPerEvictionRun;@b@    this.minEvictTime = minEvictTime;@b@    this.testWhileIdle = testWhileIdle;@b@    this.url = url;@b@    this.username = username;@b@    this.password = password;@b@    this.name = name;@b@    this.validationQuery = validationQuery;@b@    this.driver = driver;@b@    this.backupUrl = backupUrl;@b@    this.cachedPreparedStatements = cachedPreparedStatements;@b@  }@b@@b@  public Object clone()@b@  {@b@    Config clone = new Config(this.maxActive, this.exhausted, this.maxWait, this.maxIdle, this.testOnBorrow, this.testOnReturn, this.timeBetweenEvictRun, this.numTestsPerEvictionRun, this.minEvictTime, this.testWhileIdle, this.url, this.username, this.password, this.name, this.validationQuery, this.driver, this.backupUrl, this.cachedPreparedStatements);@b@@b@    return clone;@b@  }@b@@b@  public int getMaxActive() {@b@    return this.maxActive;@b@  }@b@@b@  public void setMaxActive(int maxActive) {@b@    this.maxActive = maxActive;@b@  }@b@@b@  public byte getExhausted() {@b@    return this.exhausted;@b@  }@b@@b@  public void setExhausted(byte exhausted) {@b@    this.exhausted = exhausted;@b@  }@b@@b@  public long getMaxWait() {@b@    return this.maxWait;@b@  }@b@@b@  public void setMaxWait(long maxWait) {@b@    this.maxWait = maxWait;@b@  }@b@@b@  public int getMaxIdle() {@b@    return this.maxIdle;@b@  }@b@@b@  public void setMaxIdle(int maxIdle) {@b@    this.maxIdle = maxIdle;@b@  }@b@@b@  public boolean isTestOnBorrow() {@b@    return this.testOnBorrow;@b@  }@b@@b@  public void setTestOnBorrow(boolean testOnBorrow) {@b@    this.testOnBorrow = testOnBorrow;@b@  }@b@@b@  public boolean isTestOnReturn() {@b@    return this.testOnReturn;@b@  }@b@@b@  public void setTestOnReturn(boolean testOnReturn) {@b@    this.testOnReturn = testOnReturn;@b@  }@b@@b@  public long getTimeBetweenEvictRun() {@b@    return this.timeBetweenEvictRun;@b@  }@b@@b@  public void setTimeBetweenEvictRun(long timeBetweenEvictRun) {@b@    this.timeBetweenEvictRun = timeBetweenEvictRun;@b@  }@b@@b@  public int getNumTestsPerEvictionRun() {@b@    return this.numTestsPerEvictionRun;@b@  }@b@@b@  public void setNumTestsPerEvictionRun(int numTestsPerEvictionRun) {@b@    this.numTestsPerEvictionRun = numTestsPerEvictionRun;@b@  }@b@@b@  public void setMinEvictTime(long minEvictTime) {@b@    this.minEvictTime = minEvictTime;@b@  }@b@@b@  public long getMinEvictTime() {@b@    return this.minEvictTime;@b@  }@b@@b@  public boolean isTestWhileIdle() {@b@    return this.testWhileIdle;@b@  }@b@@b@  public void setTestWhileIdle(boolean testWhileIdle) {@b@    this.testWhileIdle = testWhileIdle;@b@  }@b@@b@  public String getUrl() {@b@    return this.url;@b@  }@b@@b@  public void setUrl(String url) {@b@    this.url = url;@b@  }@b@@b@  public String getName() {@b@    return this.name;@b@  }@b@@b@  public void setName(String name) {@b@    this.name = name;@b@  }@b@@b@  public String getValidationQuery() {@b@    return this.validationQuery;@b@  }@b@@b@  public void setValidationQuery(String validationQuery) {@b@    this.validationQuery = validationQuery;@b@  }@b@@b@  public String getDriver() {@b@    return this.driver;@b@  }@b@@b@  public void setDriver(String driver) {@b@    this.driver = driver;@b@  }@b@@b@  public String getUsername() {@b@    return this.username;@b@  }@b@@b@  public void setUsername(String username) {@b@    this.username = username;@b@  }@b@@b@  public String getPassword() {@b@    return this.password;@b@  }@b@@b@  public void setPassword(String password) {@b@    this.password = password;@b@  }@b@@b@  public String getBackupUrl() {@b@    return this.backupUrl;@b@  }@b@@b@  public void setBackupUrl(String backupUrl) {@b@    this.backupUrl = backupUrl;@b@  }@b@@b@  public String getWorkingUrl() {@b@    return this.workingUrl;@b@  }@b@@b@  public void setWorkingUrl(String workingUrl) {@b@    this.workingUrl = workingUrl;@b@  }@b@@b@  public int getCachedPreparedStatements() {@b@    return this.cachedPreparedStatements;@b@  }@b@@b@  public void setCachedPreparedStatements(int cachedPreparedStatements) {@b@    this.cachedPreparedStatements = cachedPreparedStatements;@b@  }@b@@b@  public String toString()@b@  {@b@    StringBuffer sb = new StringBuffer();@b@@b@    sb.append("name: " + this.name + "\n");@b@    sb.append("driver: " + this.driver + "\n");@b@    sb.append("url: " + this.url + "\n");@b@    sb.append("backupUrl: " + this.backupUrl + "\n");@b@    sb.append("workingUrl: " + this.workingUrl + "\n");@b@    sb.append("username: " + this.username + "\n");@b@    sb.append("password: " + this.password + "\n");@b@    sb.append("validationQuery: " + this.validationQuery + "\n");@b@    sb.append("maxActive: " + this.maxActive + "\n");@b@    sb.append("exhausted: " + this.exhausted + "\n");@b@    sb.append("maxWait: " + this.maxWait + "\n");@b@    sb.append("maxIdle: " + this.maxIdle + "\n");@b@    sb.append("testOnBorrow: " + this.testOnBorrow + "\n");@b@    sb.append("testOnReturn: " + this.testOnReturn + "\n");@b@    sb.append("timeBetweenEvictRun: " + this.timeBetweenEvictRun + "\n");@b@    sb.append("numTestsPerEvictionRun: " + this.numTestsPerEvictionRun + "\n");@b@    sb.append("minEvictTime: " + this.minEvictTime + "\n");@b@    sb.append("testWhileIdle: " + this.testWhileIdle + "\n");@b@    sb.append("cachedPreparedStatements: " + this.cachedPreparedStatements + "\n");@b@@b@    return sb.toString();@b@  }@b@}

oracle数据库中is的使用说明,分享数据库工具类DatabaseUtils实现数据库登录验证isUrlValid、数据类型判断isOracle及连接关闭closeDbObject等常见操作...相关推荐

  1. oracle转换当前时间,oracle数据库中时间转化方法分享

    摘要: 下文讲述oracle数据库中时间转化方法分享,如下所示: 一.toChar 1.取得当前日期是本月的第几周 select to_char(sysdate,'W') from dual; sel ...

  2. android 如何从服务器端的数据库中拿数据,在客户端显示类?

    ============问题描述============ android 如何从服务器端的数据库中拿数据,在客户端显示类? ============解决方案1============ 写一个网络访问的 ...

  3. 【审批工作流camunda教程】(二):camunda数据库中的48张表分别的大致含义,数据库表结构介绍

    教程一: 创建camunda项目=>部署流程定义=>创建流程实例=>走完流程实例 教程二: camunda数据库中的47张表分别的大致含义,数据库表结构介绍 教程三: 下载camun ...

  4. ShareIntentUtil【调用系统自带的分享的工具类】

    版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 根据参考资料的文章,整理了调用系统自带分享的工具类(实现了适配7.0FileProvider的功能),需要搭配<Android ...

  5. android 对象数据库中,解析嵌套的JSON对象,并存储在数据库中的Android

    我只是试图让存储在我的JSON文件中的值,并将其保存到SQLite数据库:解析嵌套的JSON对象,并存储在数据库中的Android 这是我的JSON文件: { "list": { ...

  6. html中显示数据库中的一条数据,如何使用html表显示数据库中的数据

    我正在尝试在HTML表中显示数据库中的数据.主要问题是:该表未出现. Id String Array // Mostrar dados echo " { $databaseName = 'v ...

  7. mysql数据库中 索引的作用是什么意思_什么是数据库索引,索引有什么作用

    数据库索引是为了增加查询速度而对表字段附加的一种标识.见过很多人机械的理解索引的概念,认为增加索引只有好处没有坏处.这里想把之前的索引学习笔记总结一下: 首先明白为什么索引会增加速度,DB在执行一条S ...

  8. Winform中实现执行cmd命令的工具类

    场景 Winform中执行cmd命令的工具类,比如调用某些exe,类似mysqldump.exe这样类似的命令. 新建工具类CmdHelper using System; using System.C ...

  9. Android+Java中使用Aes对称加密的工具类与使用

    场景 Android+Java中使用RSA加密实现接口调用时的校验功能: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/11146 ...

最新文章

  1. MySQL实战45讲学习笔记:MySQL架构(第一讲)
  2. centos7标准版命令界面和图形界面相互切换
  3. 打开微型计算机的电源时,计算机操作与使用试题(有答案)
  4. 【差分】bzoj 1676 [Usaco2005 Feb]Feed Accounting 饲料计算
  5. alv 刷新_钜献 | 60小时刷新你的雅思托福成绩!明早9点我们要搞事情了!
  6. stc单片机485发送多出一字节_单片机干货!STC8H案例制作分享(内含高清实物动图)...
  7. 关于解决service 'sapdp00' not found的办法
  8. Linux开启服务器问题(李蕾问题)
  9. linux给目录加裸设备磁盘,Linux添加裸设备空间(Redhat)
  10. 安装JDK,如何配置PATH,如何配置CLASSPATH
  11. javascript简介和基本语法
  12. [Javascript]js中所学知识点回顾总结
  13. springmvc(2)Controller源码简单解析
  14. WinRM(Windows远程管理)介绍,Python远程操作Windows
  15. “职”等你来!阿里云2020年专场招聘来了
  16. math ceil函数python_Python ceil函数
  17. 甲骨文发布移动版Java发展路线图
  18. 领导力专题︱如何培养与提升领导力
  19. django+前端+数据库 week1
  20. 互联网那些名词~~~

热门文章

  1. 优思学院(香港)是真的吗?
  2. 在终端画画、炒股、玩游戏
  3. java 读取 指纹_java代码怎么获取数字的证书那一串20位指纹?
  4. Unity角色残影特效支持gpuInstancing
  5. 英语四六级翻译2:汉语
  6. 后门攻击阅读笔记,Graph Backdoor
  7. 火狐 load xpcom_为Firefox浏览器创建并实现XPCOM组件
  8. sd卡分区 提示sd卡损坏_如何从损坏的SD卡中恢复图像
  9. 前程无忧2021年查证并拦截涉嫌违法违规职位28.4万个
  10. zedboard 音频芯片 adau1761 linux 下音频驱动(三)PetaLinux