Scala Database Tools Class

Source: Internet
Author: User

Scala's database connection pool, based on MySQL

Import Java.util.concurrent.ConcurrentHashMapimport COM.JOLBOX.BONECP. {bonecpconfig, BONECP}import java.util.ResourceBundleimport java.util.LinkedListimport Java.sql.DriverManagerimport java.sql.Connection/** Database Connection Pool Tool class * Language: Scala * time: 2016-07-09*/Object Dbconnectionpool {private Val Reader= Resourcebundle.getbundle ("Connection") Private Val max_connection= Reader.getstring ("Jeecg.max_connection")//total number of connection poolsPrivate Val connection_num = reader.getstring ("Jeecg.connection_num")//number of connections generatedPrivatevarCurrent_num = 0//the number of connections that the current connection pool has generatedPrivate Val Pools =NewLinkedlist[connection] ()//Connection PoolPrivate Val Driver = reader.getstring ("Jeecg.driver") Private Val url= Reader.getstring ("Jeecg.url") Private Val username= Reader.getstring ("Jeecg.username") Private Val Password= Reader.getstring ("Jeecg.password") /** * Load driver*/private def before () {if(Current_num > Max_connection.toint &&Pools.isempty ()) {Print ("Busyness") Thread.Sleep (2000) before ()}Else{class.forname (Driver)}}/** * Get Connected*/Private Def initconn (): Connection={val conn=drivermanager.getconnection (URL, username, password) conn}/** * Initialize connection pool*/Private Def initconnectionpool (): Linkedlist[connection]={anyref.synchronized ({if(Pools.isempty ()) {before () for(I <-1to Connection_num.toint) {Pools.push (Initconn ()) Current_num+ = 1}} pools})}/** * Get Connected*/def getconn (): Connection={Initconnectionpool () Pools.poll ()}/** * Release connection*/def releasecon (con:connection) {Pools.push (con)}}

Configuration file

#数据库连接池配置文件jeecg. Driver=org.gjt.mm.mysql.Driverjeecg.url=jdbc\:mysql\://0.0.0.0 \:3306/jeecg?useunicode=true&characterencoding=utf-8jeecg.username=rootjeecg.password=**** jeecg.max_connection=8jeecg.connection_num=10

DAO class

import java.sql.ResultSetimport java.sql.PreparedStatementimport java.sql.Connectionimport java.sql.Statement /** Data Manipulation Tool class * Language: Scala * time: 2016-07-09*/private[org] abstract class Basedao[t] (conn:connection) {/** * INSERT data * @param SQL SQL statement * @param params parameter list * @param convert primary Key conversion method * @return Conversion result*/protected def insert[t] (sql:string, Params:array[any]) (Convert:resultset= + T) ={val pstmt=conn preparestatement (SQL, Statement.return_generated_keys) setparameters (pstmt, params) pstmt.executeupdate Val Rs=Pstmt.getgeneratedkeys Rs.next Convert (RS)}/** * UPDATE data * @param SQL SQL statement * @param params parameter list * @return affect the number of rows*/protected def update (sql:string, Params:array[any])=createstatement (SQL, params). Executeupdate/** * Query Object * @param SQL SQL statement * @param params parameter list * @param convert result set conversion method * @return Generic Object*/protected def queryforobject[t] (sql:string, Params:array[any]) (Convert:resultset= + T) ={val rs=query (SQL, params)if(rs.next) {val result=convert (RS)if(Rs.next) {val ex=NewresultstoomanyexceptionThrowex}ElseSome (Result)}ElseNone}/** * Query Object list * @param SQL SQL statement * @param params parameter list * @param convert result set conversion method * @return Generic Object list*/protected def queryforlist[t] (sql:string, Params:array[any]) (Convert:resultset= + T) ={val rs=query (SQL, params)varResults =List[t] () while(rs.next) {results = results: +convert (RS)} results}/** * Query Object map * @param SQL SQL statement * @param params parameter list * @param convert result set conversion method * @return Generic Object Mappings*/protected def queryformap[k, V] (sql:string, Params:array[any]) (Convert:resultset= = (K, V)) ={val rs=query (SQL, params)varResults =map[k, V] () while(Rs.next) {results + =convert (RS)} results}/** * query * @param SQL SQL statement * @param params parameter list*/Private def query (Sql:string, Params:array[any])=createstatement (SQL, params). ExecuteQuery/** * Create declaration * @param SQL SQL statement * @param params parameter list*/Private def createstatement (Sql:string, Params:array[any])={val pstmt=conn Preparestatement SQL Setparameters (pstmt, params) pstmt}/** * Insert parameter * @param pstmt precompiled Declaration * @param params parameter list*/Private def setparameters (Pstmt:preparedstatement, Params:array[any]) { for(I <-1 to Params.length) {pstmt setobject (i, params (i-1)) }  }}/** * Result value reader*/Object Resultvaluegetter {/** * Query Result value * @param rs result set * @param getresult how to get a single value result * @return value*/def Getresultvalue[t] (rs:resultset) (Getresult:resultset= + T) ={val result=GetResult (RS)if(rs.wasnull) NoneElseSome (Result)}/** * Get the value of the string result * @param rs result set * @param colnum column number*/def getstringvalue (Rs:resultset, Colnum:int)=Getresultvalue (RS) {_ GetString Colnum}/** * Get the value of the string result * @param rs result set * @param colnum column number*/def getintvalue (Rs:resultset, Colnum:int)=Getresultvalue (RS) {_ GetInt Colnum}/** * Get the value of the string result * @param rs result set * @param colnum column number*/def getlongvalue (Rs:resultset, Colnum:int)=Getresultvalue (RS) {_ Getlong Colnum}/** * Get the value of the string result * @param rs result set * @param colnum column number*/def getdoublevalue (Rs:resultset, Colnum:int)=Getresultvalue (RS) {_ GetDouble Colnum}/** * Get the value of the string result * @param rs result set * @param colnum column number*/def getbooleanvalue (Rs:resultset, Colnum:int)=Getresultvalue (RS) {_ Getboolean Colnum}/** * Get the value of the string result * @param rs result set * @param colnum column number*/def gettimestampvalue (Rs:resultset, Colnum:int)=Getresultvalue (RS) {_ Gettimestamp Colnum}/** * Get the value of the string result * @param rs result set * @param colname column name*/def getstringvalue (Rs:resultset, colname:string)=Getresultvalue (RS) {_ GetString colname}/** * Get the value of the string result * @param rs result set * @param colname column name*/def getintvalue (Rs:resultset, colname:string)=Getresultvalue (RS) {_ GetInt colname}/** * Get the value of the string result * @param rs result set * @param colname column name*/def getlongvalue (Rs:resultset, colname:string)=Getresultvalue (RS) {_ Getlong colname}/** * Get the value of the string result * @param rs result set * @param colname column name*/def getdoublevalue (Rs:resultset, colname:string)=Getresultvalue (RS) {_ GetDouble colname}/** * Get the value of the string result * @param rs result set * @param colname column name*/def getbooleanvalue (Rs:resultset, colname:string)=Getresultvalue (RS) {_ Getboolean colname}/** * Get the value of the string result * @param rs result set * @param colname column name*/def gettimestampvalue (Rs:resultset, colname:string)=Getresultvalue (RS) {_ Gettimestamp ColName}}/** * Results too many exceptions*/class Resultstoomanyexception extends Exception ("Returned too many results.") {}

Scala Database Tools Class

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.