object mysql{
//加载驱动
val driver = "com.mysql.cj.jdbc.Driver"
//jdbc连接方式,mysql主机ip地址,mysql端口默认3306,数据库名称
val url = "jdbc.mysql://172.16.24.200:3306/dete"
//主机用户名字
val userName = "root"
//主机用户密码
val password = "123456"
//生成connection对象
var connection : Connection=null
//创建getconnection方法
def getConnection():Unit={
//传进驱动
Class.forName(driver)
//传进mysql信息
connection = DriverManger.getConnection(url,userName,password)
}
//创建main方法
def main(args:Array[String]):Unit ={
//调用connection方法
getconnection()
//打印connection方法内容
println(connection)
}
}
Scala连接数据库
最新推荐文章于 2024-05-28 17:45:14 发布