studyScala

在scala中連接mysql數據庫

package com.sglnetwork.jdbcStudy

 

import java.sql._

 

 

object ConnectionFactory{
  val driver: String = "com.mysql.jdbc.Driver"
  val url: String = "jdbc:mysql://localhost:3306/school"
  val user:String = "root"
  val passwd: String = "sglnetwork"
  var conn: Connection = null
  def getConn(): Connection = {
    Class.forName(driver)
    conn = DriverManager.getConnection(url,user,passwd)
    conn
  }
  def close(st:Statement,pstm : PreparedStatement ,rs:ResultSet ,conn : Connection) : Unit = {
    if(st!=null){
      st.close
    }
    if(pstm!=null){
      pstm.close
    }
    if(rs!=null){
      rs.close
    }
    if(conn!=null){
      conn.close
    }
  }
}

 

以後使用連接的時候就可以直接使用,想在java中使用中一樣

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章