当前位置:网站首页>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中走一遍看看有没有错误,列明不识别等小问题
边栏推荐
猜你喜欢
随机推荐
C语言日记 5、7setprecision()问题
What's wrong with running yolov5 (1) p, r, map are all 0
8576 Basic operations of sequential linear tables
宝塔搭建DM企业建站系统源码实测
Kubernetes介绍
Hession使用
Visual Studio配置OpenCV之后,提示:#include<opencv2/opencv.hpp>无法打开源文件
jwt (json web token)
getUserProfile接口不显示用户性别和地区
The IDEA of packaged jar package
C语言初级—判断一个数是不是素数(函数封装)
HBuilderX 核心插件安装提示:“插件XXX下载失败,请检查网络”问题的解决办法
鼠标右键菜单栏太长如何减少
mysql
verilog学习|《Verilog数字系统设计教程》夏宇闻 第三版思考题答案(第十一章)
VS Code无法安装插件之Unable to install because, the extension '' compatible with current version
C语言一维数组练习——将一个字符串中的某个字符替换成其它字符
static关键字3种作用,简单粗暴对比,好理解
2022最新交规记忆重点
芝诺悖论的理解