studyScala

来源:互联网 发布:人气最高的网络主播 编辑:程序博客网 时间:2024/04/28 20:15

在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中使用中一样

 

原创粉丝点击