scala-jdbc连接池和工具类

连接池

import java.sql.{Connection, DriverManager}
import java.util.{LinkedList, ResourceBundle}

import org.apache.spark.Logging

/**
  * 数据库连接池工具类
  */
object DBConnectionPool extends Logging{
  private val reader = ResourceBundle.getBundle("properties/version_manager_jdbc")
  private val max_connection = reader.getString("max_connection") //连接池总数
  private val connection_num = reader.getString("connection_num") //产生连接数
  private var current_num = 0 //当前连接池已产生的连接数
  private val pools = new LinkedList[Connection]() //连接池
  private val driver = reader.getString("driver")
  private val url = reader.getString("url")
  private val username = reader.getString("username")
  private val password = reader.getString("password")
  /**
    * 加载驱动
    */
  private def before() {
    if (current_num > max_connection.toInt && pools.isEmpty()) {
      println("busyness")
      Thread.sleep(2000)
      before()
    } else {
      Class.forName(driver)
    }
  }
  /**
    * 获得连接
    */
  private def initConn(): Connection = {
    val conn = DriverManager.getConnection(url, username, password)
//    logError(url)
    conn
  }


  /**
    * 初始化连接池
    */
  private def initConnectionPool(): LinkedList[Connection] = {
    AnyRef.synchronized({
      if (pools.isEmpty()) {
        before()
        for (i <- 1 to connection_num.toInt) {
          pools.push(initConn())
          current_num += 1
        }
      }
      pools
    })
  }
  /**
    * 获得连接
    */
  def getConn():Connection={
    initConnectionPool()
    pools.poll()
  }
  /**
    * 释放连接
    */
  def releaseCon(con:Connection){
    pools.push(con)
  }


}

工具类


import java.sql.{CallableStatement, Connection, PreparedStatement, ResultSet, SQLException, Statement, Types}



import scala.collection.mutable.ListBuffer

/**
  *

  * @define

  */
object JdbcHelper {

  private var conn: Connection = null
  private var preparedStatement: PreparedStatement = null
  private var callableStatement: CallableStatement = null


  /**
    * 建立数据库连接
    *
    * @return
    * @throws SQLException
    */
  @throws[SQLException]
  private def getConnection: Connection = {
    conn = DBConnectionPool.getConn()
    conn
  }

  /**
    * 释放连接
    *
    * @param conn
    */
  private def freeConnection(conn: Connection) = {
    try
      DBConnectionPool.releaseCon(conn)
    catch {
      case e: SQLException =>
        e.printStackTrace()
    }
  }

  /**
    * 释放statement
    *
    * @param statement
    */
  private def freeStatement(statement: Statement) = {
    try
      statement.close()
    catch {
      case e: SQLException =>
        e.printStackTrace()
    }
  }

  /**
    * 释放resultset
    *
    * @param rs
    */
  private def freeResultSet(rs: ResultSet) = {
    try
      rs.close()
    catch {
      case e: SQLException =>
        e.printStackTrace()
    }
  }


  /**
    * 释放资源
    *
    * @param conn
    * @param statement
    * @param rs
    */
  def free(conn: Connection, statement: Statement, rs: ResultSet): Unit = {
    if (rs != null) freeResultSet(rs)
    if (statement != null) freeStatement(statement)
    if (conn != null) freeConnection(conn)
  }

  /

  /**
    * 获取PreparedStatement
    *
    * @param sql
    * @throws SQLException
    */
  @throws[SQLException]
  private def getPreparedStatement(sql: String) = {
    conn = getConnection
    preparedStatement = conn.prepareStatement(sql)
  }

  /**
    * 用于查询,返回结果集
    *
    * @param sql
    * sql语句
    * @return 结果集
    * @throws SQLException
    */
  @throws[SQLException]
  def query(sql: String): List[Map[String, Object]] = {
    var rs: ResultSet = null
    try {
      getPreparedStatement(sql)
      rs = preparedStatement.executeQuery
      ResultToListMap(rs)
    } catch {
      case e: SQLException =>
        throw new SQLException(e)
    } finally free(conn, callableStatement, rs)
  }


  /**
    * 用于带参数的查询,返回结果集
    *
    * @param sql
    * sql语句
    * @param paramters
    * 参数集合
    * @return 结果集
    * @throws SQLException
    */
  @throws[SQLException]
  def query(sql: String, paramters: Any*): List[Map[String, Object]] = {
    var rs: ResultSet = null
    try {
      getPreparedStatement(sql)
      var i = 0
      while ( {
        i < paramters.length
      }) {
        preparedStatement.setObject(i + 1, paramters(i))

        {
          i += 1;
          i - 1
        }
      }
      rs = preparedStatement.executeQuery
      ResultToListMap(rs)
    } catch {
      case e: SQLException =>
        throw new SQLException(e)
    } finally free(conn, callableStatement, rs)
  }


  /**
    * 返回单个结果的值,如count\min\max等等
    *
    * @param sql
    * sql语句
    * @return 结果集
    * @throws SQLException
    */
  @throws[SQLException]
  def getSingle(sql: String): Any = {
    var result: Any = null
    var rs: ResultSet = null
    try {
      getPreparedStatement(sql)
      rs = preparedStatement.executeQuery
      if (rs.next) result = rs.getObject(1)
      result
    } catch {
      case e: SQLException =>
        throw new SQLException(e)
    } finally free(conn, callableStatement, rs)
  }

  /**
    * 返回单个结果值,如count\min\max等
    *
    * @param sql
    * sql语句
    * @param paramters
    * 参数列表
    * @return 结果
    * @throws SQLException
    */
  @throws[SQLException]
  def getSingle(sql: String, paramters: Any*): Any = {
    var result: Any = null
    var rs: ResultSet = null
    try {
      getPreparedStatement(sql)
      var i = 0
      while ( {
        i < paramters.length
      }) {
        preparedStatement.setObject(i + 1, paramters(i))

        {
          i += 1;
          i - 1
        }
      }
      rs = preparedStatement.executeQuery
      if (rs.next) result = rs.getObject(1)
      result
    } catch {
      case e: SQLException =>
        throw new SQLException(e)
    } finally free(conn, callableStatement, rs)
  }

  /**
    * 用于增删改
    *
    * @param sql
    * sql语句
    * @return 影响行数
    * @throws SQLException
    */
  @throws[SQLException]
  def update(sql: String): Int = try {
    getPreparedStatement(sql)
    preparedStatement.executeUpdate
  } catch {
    case e: SQLException =>
      throw new SQLException(e)
  } finally free(conn, callableStatement, null)

  /**
    * 用于增删改(带参数)
    *
    * @param sql
    * sql语句
    * @param paramters
    * sql语句
    * @return 影响行数
    * @throws SQLException
    */
  @throws[SQLException]
  def update(sql: String, paramters: Any*): Int = try {
    getPreparedStatement(sql)
    var i = 0
    while ( {
      i < paramters.length
    }) {
      preparedStatement.setObject(i + 1, paramters(i))

      {
        i += 1;
        i - 1
      }
    }
    preparedStatement.executeUpdate
  } catch {
    case e: SQLException =>
      throw new SQLException(e)
  } finally free(conn, callableStatement, null)

  /**
    * 插入值后返回主键值
    *
    * @param sql
    * 插入sql语句
    * @return 返回结果
    * @throws Exception
    */
  @throws[SQLException]
  def insertWithReturnPrimeKey(sql: String): Any = {
    var rs: ResultSet = null
    var result: Object = null
    try {
      conn = getConnection
      preparedStatement = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)
      preparedStatement.execute
      rs = preparedStatement.getGeneratedKeys
      if (rs.next) result = rs.getObject(1)
      result
    } catch {
      case e: SQLException =>
        throw new SQLException(e)
    }
  }

  /**
    * 插入值后返回主键值
    *
    * @param sql
    * 插入sql语句
    * @param paramters
    * 参数列表
    * @return 返回结果
    * @throws SQLException
    */
  @throws[SQLException]
  def insertWithReturnPrimeKey(sql: String, paramters: Any*): Any = {
    var rs: ResultSet = null
    var result: Object = null
    try {
      conn = getConnection
      preparedStatement = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)
      var i = 0
      while ( {
        i < paramters.length
      }) {
        preparedStatement.setObject(i + 1, paramters(i))

        {
          i += 1;
          i - 1
        }
      }
      preparedStatement.execute
      rs = preparedStatement.getGeneratedKeys
      if (rs.next) result = rs.getObject(1)
      result
    } catch {
      case e: SQLException =>
        throw new SQLException(e)
    }
  }

  //

  /**
    * 获取CallableStatement
    *
    * @param procedureSql
    * @throws SQLException
    */
  @throws[SQLException]
  private def getCallableStatement(procedureSql: String) = {
    conn = getConnection
    conn.prepareCall(procedureSql)
  }


  /**
    * 存储过程查询
    * 注意outNames和outOracleTypes的顺序要对应 顺序按存储过程的参数顺序排列
    *
    * @param procedureSql
    * @param ins            输入参数数组
    * @param outNames       输出参数名称
    * @param outOracleTypes 输出参数类型
    * @return
    *
    */
  @throws[SQLException]
  def callableQuery(procedureSql: String, ins: Array[Object], outNames: Array[String], outOracleTypes: Array[Int]): Map[String, Object] = {

    val listBuffer = new ListBuffer[Object]

    try {
      callableStatement = getCallableStatement(procedureSql)

      var count = 0

      for (i <- 0 until ins.length) {
        count = count + 1
        callableStatement.setObject(count, ins(i))
      }

      for (j <- 0 until outOracleTypes.length) {
        count = count + 1
        callableStatement.registerOutParameter(count, outOracleTypes(j))
      }

      callableStatement.execute()

      count = count - outOracleTypes.length
      for (i <- 0 until outOracleTypes.length) {
        count = count + 1
        listBuffer.append(callableStatement.getObject(count))
      }
      outNames.zip(listBuffer.toList).toMap
    } catch {
      case e: SQLException =>
        throw new SQLException(e)
    }
    finally free(conn, callableStatement, null)
  }


  /**
    * 调用存储过程,查询单个值
    *
    * @param procedureSql
    * @return
    * @throws SQLException
    */
  @throws[SQLException]
  def callableGetSingle(procedureSql: String): Any = {
    var result: Any = null
    var rs: ResultSet = null
    try {
      rs = getCallableStatement(procedureSql).executeQuery
      while ( {
        rs.next
      }) result = rs.getObject(1)
      result
    } catch {
      case e: SQLException =>
        throw new SQLException(e)
    } finally free(conn, callableStatement, rs)
  }

  /**
    * 调用存储过程(带参数),查询单个值
    *
    * @param procedureSql
    * @return
    * @throws SQLException
    */
  @throws[SQLException]
  def callableGetSingle(procedureSql: String, paramters: Any*): Any = {
    var result: Any = null
    var rs: ResultSet = null
    try {
      callableStatement = getCallableStatement(procedureSql)
      var i = 0
      while ( {
        i < paramters.length
      }) {
        callableStatement.setObject(i + 1, paramters(i))

        {
          i += 1;
          i - 1
        }
      }
      rs = callableStatement.executeQuery
      while ( {
        rs.next
      }) result = rs.getObject(1)
      result
    } catch {
      case e: SQLException =>
        throw new SQLException(e)
    } finally free(conn, callableStatement, rs)
  }

  @throws[SQLException]
  def callableWithParamters(procedureSql: String): Any = try {
    callableStatement = getCallableStatement(procedureSql)
    callableStatement.registerOutParameter(0, Types.OTHER)
    callableStatement.execute
    callableStatement.getObject(0)
  } catch {
    case e: SQLException =>
      throw new SQLException(e)
  } finally free(conn, callableStatement, null)

  /**
    * 调用存储过程,执行增删改
    *
    * @param procedureSql
    * 存储过程
    * @return 影响行数
    * @throws SQLException
    */
  @throws[SQLException]
  def callableUpdate(procedureSql: String): Int = try {
    callableStatement = getCallableStatement(procedureSql)
    callableStatement.executeUpdate
  } catch {
    case e: SQLException =>
      throw new SQLException(e)
  } finally free(conn, callableStatement, null)

  /**
    * 调用存储过程(带参数),执行增删改
    *
    * @param procedureSql
    * 存储过程
    * @param parameters
    * @return 影响行数
    * @throws SQLException
    */
  @throws[SQLException]
  def callableUpdate(procedureSql: String, parameters: Any*): Int = try {
    callableStatement = getCallableStatement(procedureSql)
    var i = 0
    while ( {
      i < parameters.length
    }) {
      callableStatement.setObject(i + 1, parameters(i))

      {
        i += 1;
        i - 1
      }
    }
    callableStatement.executeUpdate
  } catch {
    case e: SQLException =>
      throw new SQLException(e)
  } finally free(conn, callableStatement, null)


  @throws[SQLException]
  private def ResultToListMap(rs: ResultSet) = {

    val list = new ListBuffer[Map[String, Object]]

    while (rs.next) {
      val map = new scala.collection.mutable.HashMap[String, Object]
      val md = rs.getMetaData
      for (i <- 1 until md.getColumnCount) {
        map.put(md.getColumnLabel(i), rs.getObject(i))
      }
      list.append(map.toMap)
    }
    list.toList
  }


  def main(args: Array[String]): Unit = {
    //        val list = query("select * from POL_BEN_PLAN_CFG")
    //        for (e <- list) {
    //          println(e)
    //        }

//    val startVersionMap = VersionManager.verStartIf("20181106", "spark_ifrs17_mp_ind_aio")
//    println(startVersionMap.toString())
//
//    val endVersionMap = VersionManager.verEndIf(startVersionMap, JobConstants.P_RUN_FLAG_SUCCESS, "success")
//    println(endVersionMap.toString())

  }

} 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

猿与禅

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值