1.功能簡述:
通過scala代碼對Mysql數據庫進行JDBC連接
2.源代碼
import java.sql.{Connection, DriverManager, ResultSet} object Mysql { //定義驅動,數據庫地址,名稱,密碼 private val driver="com.mysql.cj.jdbc.Driver" //根據使用的jar包版本進行更改,我用的為高版本 private val url="jdbc:mysql://ip:3306/databasename?serverTimezone=UTC" //ip為數據庫所在ip,若為本機數據庫則是localhost;databasename為要鏈接的數據庫名 private val userName="root" //連接所用用戶名 private val password="123456" //連接所用密碼 //獲取連接 private var connection:Connection=null def getConnection():Unit={ Class.forName(driver) connection =DriverManager.getConnection(url,userName,password) } //測試 def main(args:Array[String]):Unit={ getConnection() println(connection) } }