Scalaカスタムデータベース接続プールおよびツールクラス
DBUtil.scala
添付:コンフィギュレーションファイル(conf/db.properties)
import java.sql.{Connection, DriverManager, PreparedStatement, ResultSet, Statement}
import java.util.concurrent.{BlockingQueue, LinkedBlockingQueue}
/**
* JDBC
* @author Created by CN on 2018/12/5/0005 11:09 .
*/
class DBUtil {
private var connection: Connection = _
private var preparedStatement: PreparedStatement = _
private var resultSet: ResultSet = _
/**
* SQL
* @param sql sql
*/
def execute(sql: String): Unit = {
try {
this.connection = DBUtil.getConnection
this.preparedStatement = this.connection.prepareStatement(sql)
this.preparedStatement.execute()
} catch {
case e: Exception => e.printStackTrace()
} finally {
DBUtil.returnConnection(this.connection)
this.connection == null
}
}
/**
*
* @param sql
* @param objects
* @return
*/
def insertForGeneratedKeys(sql: String, objects: Array[Any]): ResultSet = {
try {
this.connection = DBUtil.getConnection
this.preparedStatement = this.connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)
if(objects != null)
this.setPreparedStatement(objects)
this.preparedStatement.executeUpdate()
this.resultSet = preparedStatement.getGeneratedKeys
} catch {
case e: Exception => e.printStackTrace()
} finally {
DBUtil.returnConnection(this.connection)
this.connection == null
}
this.resultSet
}
/**
*
* @param sql sql
* @param objects
* @return
*/
def executeQuery(sql: String, objects: Array[Any]): ResultSet = {
try {
this.connection = DBUtil.getConnection
this.preparedStatement = this.connection.prepareStatement(sql)
if(objects != null)
this.setPreparedStatement(objects)
this.resultSet = this.preparedStatement.executeQuery()
} catch {
case e: Exception => e.printStackTrace()
} finally {
DBUtil.returnConnection(this.connection)
this.connection == null
}
this.resultSet
}
/**
* /
* @param sql
* @param objects
* @return , -1
*/
def executeInsertOrUpdate(sql: String, objects: Array[Any]): Int = {
try {
this.connection = DBUtil.getConnection
this.preparedStatement = this.connection.prepareStatement(sql)
if(objects != null)
this.setPreparedStatement(objects)
this.preparedStatement.executeUpdate()
} catch {
case e: Exception => e.printStackTrace()
} finally {
DBUtil.returnConnection(this.connection)
this.connection == null
}
-1
}
/**
* preparedStatement
* @param objects
*/
private def setPreparedStatement(objects: Array[Any]): Unit = {
var i = 1
for(obj e.printStackTrace()
}
}
/**
*
*/
def close() : Unit = {
try {
if(resultSet != null) resultSet.close()
if(preparedStatement != null) preparedStatement.close()
// if(connection != null) connection.close()
} catch {
case e: Exception => e.printStackTrace()
}
}
}
/**
* JDBC
* config/db.properties
* @author Created by CN on 2018/11/9 17:37
*/
object DBUtil {
private val propertiesUtil = new PropertiesUtil("config/db.properties")
//
private val driverClass: String = propertiesUtil.readPropertyByKey("driverClass")
//
private val url: String = propertiesUtil.readPropertyByKey("url")
//
private val username: String = propertiesUtil.readPropertyByKey("userName")
//
private val password: String = propertiesUtil.readPropertyByKey("password")
//
Class.forName(driverClass)
//
val poolSize: Int = propertiesUtil.readPropertyByKey("poolSize").toInt
// -
private val pool: BlockingQueue[Connection] = new LinkedBlockingQueue[Connection]()
/**
*
*/
for(i 0) {
try {
// println(s" : ${DBUtil.pool.size}")
DBUtil.pool.take().close()
} catch {
case e: Exception => e.printStackTrace()
}
}
}
}
}
添付:コンフィギュレーションファイル(conf/db.properties)
driverClass = com.mysql.jdbc.Driver
url = jdbc:mysql://localhosst:3306/test?useSSL=false&useUnicode=true&characterEncoding=utf8
userName = root
password = root
poolSize = 10