标签:nic password library port let mysq star += cas
本地之前安装过MySQL
启动: mysql.server start
停止: mysql.setver stop
登录: mysql -u root ; 登录root用户。
选择一个database : use erwa
查看所有的database: show databases修改密码:格式:mysqladmin -u用户名 -p旧密码 password 新密码
例子:mysqladmin -uroot -p123456 password 123
因为换电脑了,之前是在mac上安装的,现在在windows上重新装一个
直接上代码
用的sbt,需要导入mysql-con包
libraryDependencies += "mysql" % "mysql-connector-java" % "8.0.25"
package com.erwa.test
import java.sql.{ Connection, DriverManager }
/**
* 学习通过Scala操作jdbc连接mysql数据库
*/
object operatorMysql extends App {
// 访问本地MySQL服务器,通过3306端口访问mysql数据库
val url = "jdbc:mysql://localhost:3306/db?useUnicode=true&characterEncoding=utf-8&useSSL=false"
//驱动名称
val driver = "com.mysql.cj.jdbc.Driver"
//用户名
val username = "root"
//密码
val password = "123456"
//初始化数据连接
var connection: Connection = _
try {
//注册Driver
Class.forName(driver)
//得到连接
connection = DriverManager.getConnection(url, username, password)
val statement = connection.createStatement
//执行查询语句,并返回结果
val rs = statement.executeQuery("select id, name from school")
//打印返回结果
while (rs.next) {
val id = rs.getString("id")
val name = rs.getString("name")
// println(name+"\t"+num)
println("id = %s, name = %s".format(id, name))
}
println("查询数据完成!")
// 执行插入操作
// val rs2 = statement.executeUpdate(" INSERT INTO school (id, name) VALUES (3, ‘学校3‘)")
// println("插入数据完成")
// // 执行更新操作
// val rs3 = statement.executeUpdate("UPDATE school SET id = 4, name = ‘学校4‘ where id = 3")
// println("更新数据完成!")
//
// // 执行删除操作
val rs4 = statement.executeUpdate(" delete from school where id = 4 ")
println("删除数据完成!")
println("-----执行完操作之后-----")
val rs1 = statement.executeQuery("select id, name from school")
//打印返回结果
while (rs1.next) {
val id = rs1.getString("id")
val name = rs1.getString("name")
// println(name+"\t"+num)
println("id = %s, name = %s".format(id, name))
}
println("查询数据完成!")
} catch {
case e: Exception => e.printStackTrace()
}
//关闭连接,释放资源
connection.close()
}
标签:nic password library port let mysq star += cas
原文地址:https://www.cnblogs.com/wobushitiegan/p/14929446.html