packagecn.kgc.cha01.mysqlimportscala.reflect.ClassTag//使用
object Test{
def main(args:Array[String]):Unit={
val dao =Dao()caseclassStu(name:String,sex:String,age:Int,grade:String,number:Int){
override def toString:String= s"$name,$sex,$age,$grade,$number"}
val strArrToOrder =(arr:Array[String])=>Stu(arr(0).toString,arr(1).toString,arr(2).toInt,arr(3).toString,arr(4).toInt
)
val sql ="select * from stu"
implicit val c =ClassTag(classOf[Stu])
val o:Option[Array[Stu]]= dao.select(strArrToOrder)(sql)if(o.isEmpty){println("Exption")}else{
o.get.foreach(println)}}}
packagecn.kgc.cha01.mysqlimportjava.io.{File,FileReader}importjava.sql.{Connection,DriverManager,PreparedStatement,ResultSet}importjava.util.Propertiesimportscala.collection.mutable.ArrayBufferimportscala.reflect.ClassTagclassDao(conf:Properties=null){privatevar _conf:Properties= conf
privatevar driver:String=nullprivatevar url:String=nullprivatevar username:String=nullprivatevar password:String= _
init
private def init:Unit={if(null==_conf){
_conf =newProperties()try{var path =Thread.currentThread().
getContextClassLoader.getResource("").getPath
path =newFile(path).getParentFile.getParentFile
.getParentFile.getAbsolutePath +"/config/datasource.properties"println(path)
_conf.load(newFileReader(path))
driver = _conf.getProperty("mysql.driver")
url = _conf.getProperty("mysql.url")
username = _conf.getProperty("mysql.username")
password = _conf.getProperty("mysql.password")if(null==driver ||null==url ||null==username ||null==password){println(driver,url,username,password)thrownewNullPointerException("配置有空指针")}Class.forName(driver)}catch{//这里偷懒 使用多态// case e:FileNotFoundException => println("文件找不到")// case e:NullPointerException => println("空指针异常")case e:Throwable=>{
e.printStackTrace()System.exit(-1)}}}}//排除空指针private def close(closes:AutoCloseable*)= closes.filterNot(null==_).foreach(e=>e.close())private def getCon:Connection=DriverManager.getConnection(url,username,password)private def getPst(con:Connection,sql:String,parms:Seq[Any]):PreparedStatement={
val pst = con.prepareStatement(sql)if(!parms.isEmpty){//此方法可以是一个
parms.zipWithIndex.foreach(tp2=>{
pst.setObject(tp2._2+1,tp2._1)})}
pst
}// def update(sql:String,params:Object*):Option[Int] = {// var opt:Option[Int] = null// var con:Connection = null// var pst:PreparedStatement = null// try{// con = getCon// pst = getPst(con,sql,params)// opt = Some(pst.executeUpdate())// }catch {// case e:Throwable => println(e.getMessage)// }finally {// close(pst,con)// }// opt// }
def select[T](f:Array[String]=>T)(sql:String,params:Any*)(implicit c:ClassTag[T]):Option[Array[T]]={var opt:Option[Array[T]]=Nonevar con:Connection=nullvar pst:PreparedStatement=nullvar rst:ResultSet=nulltry{
con = getCon
pst =getPst(con,sql,params)
rst = pst.executeQuery()
val buffer =ArrayBuffer[T]()
val columnCount = rst.getMetaData.getColumnCount
val arr =newArray[String](columnCount)
val seq =for(i<-0 until columnCount)yield i
while(rst.next()){
seq.foreach(ix=>arr(ix)=rst.getString(ix+1))
buffer +=f(arr)}
opt =Some(buffer.toArray(c))}catch{case e:Throwable=> e.printStackTrace()}
opt
}}
object Dao{
def apply(conf:Properties=null):Dao=newDao(conf)}
package cn.kgc.cha01.mysqlimport scala.reflect.ClassTag//使用object Test { def main(args: Array[String]): Unit = { val dao = Dao() case class Stu(name:String,sex:String,age:Int,grade:String,number:Int){ override def toString: String = s"$