当前位置:网站首页>Scala连接Mysql数据库
Scala连接Mysql数据库
2022-08-02 14:05:00 【大学生爱编程】
//添加依赖
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.49</version>
</dependency>
import java.sql.{Connection, DriverManager, PreparedStatement, ResultSet}
object Jdbc {
def main(args: Array[String]): Unit = {
//添加依赖,加载驱动
Class.forName("com.mysql.jdbc.Driver")
//建立数据库连接
val conn: Connection=DriverManager.getConnection("jdbc:mysql://192.168.5.110/bigdata?useSSL=false&useUnicode=true&characterEncoding=UTF-8","root","123456")
//编写SQL语句
val stat: PreparedStatement =conn.prepareStatement("select * from students where clazz=?")
//给参数赋值
stat.setString(1,"理科二班")
//执行查询
val resultSet: ResultSet =stat.executeQuery()
//解析数据
while(resultSet.next()){
val id: Long =resultSet.getLong("id")
val name: String =resultSet.getString("name")
val age: Long =resultSet.getLong("age")
val gender: String =resultSet.getString("gender")
val clazz: String =resultSet.getString("clazz")
println(s"$id,$name,$age,$gender,$clazz")
}
//关闭连接
stat.close()
conn.close()
}
}
可能出现的问题:
报错:
Thu Jan 06 22:38:56 CST 2022 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
解决:
url 参数直接设置 useSSL=false
jdbc:mysql://localhost:3306/数据库名?useSSL=false
url书写错误,密码账户漏写
"jdbc:mysql://192.168.5.110/bigdata?useSSL=false&useUnicode=true&characterEncoding=UTF-8","root","123456"
SQL语言在MySQL中走一遍看看有没有错误,列明不识别等小问题
边栏推荐
猜你喜欢
随机推荐
MySQL 8.0 新特性
drf view component
MySQL知识总结 (六) MySQL调优
Unit 12 associated serialization
redis delay queue
什么是 Web 3.0:面向未来的去中心化互联网
华为防火墙
鼠标右键菜单栏太长如何减少
Kubernetes架构和组件
Error Correction Design Principle of Hamming Check Code
Visual Studio配置OpenCV之后,提示:#include<opencv2/opencv.hpp>无法打开源文件
Camera Hal(Hal3)层修改Preview流
8580 Merge linked list
标签加id 和 加号 两个文本框 和一个var 赋值
宏定义问题记录day2
线性代数期末复习存档
Programming Specifications - LiteOS
华为防火墙IPS
OpenCart迁移到其他服务器
重新学习编程day1 【初始c语言】【c语言编写出计算两个数之和的代码】