当前位置:网站首页>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语言三子棋详解!!! (电脑智能下棋)(附上完整代码)

mysql常用函数

【c】大学生在校学习c语言常见代码

Implementation of redis distributed lock and watchdog

run yolov5

C语言日记 4 变量

Using the cloud GPU + pycharm training model to realize automatic background run programs, save training results, the server automatically power off

【VCU】详解S19文件(S-record)

存储系统Cache(知识点+例题)
随机推荐
C语言初级—常见问题(100~200素数,计算1+11+111+...,从键盘获取一个数并输出有几个位)
c语言三子棋详解!!! (电脑智能下棋)(附上完整代码)
The IDEA of packaged jar package
Verilog学习 系列
drf serializer - Serializer
Flask-RESTful request response and SQLAlchemy foundation
[ROS] (02) Create & compile ROS package Package
Error Correction Design Principle of Hamming Check Code
科创知识年度盛会,中国科创者大会8月6日首场开幕!
Raj delivery notes - separation 第08 speak, speaking, reading and writing
A little thought about password encryption
Network pruning (1)
【Camera2】由Camera2 特性想到的有关MED(多场景设备互动)的场景Idea
C语言初级—判断一个数是不是素数(函数封装)
St. Regis Takeaway Notes - Lecture 10 Swagger
Unit 6 meet ORM
PostgreSQL 性能谜题
C语言日记 2 标识符
宏定义问题记录day2
Deep learning framework pytorch rapid development and actual combat chapter3