当前位置:网站首页>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中走一遍看看有没有错误,列明不识别等小问题
边栏推荐
- Flask framework
- Flask-SQLAlchemy
- Flask-RESTful request response and SQLAlchemy foundation
- YOLOv7 uses cloud GPU to train its own dataset
- 【c】小游戏---扫雷雏形
- 标签加id 和 加号 两个文本框 和一个var 赋值
- 主存储器(二)
- Steps to connect the virtual machine with xshell_establish a network connection between the host and the vm virtual machine
- C语言日记 5、7setprecision()问题
- 芝诺悖论的理解
猜你喜欢
随机推荐
Ehcache基础学习
verilog学习|《Verilog数字系统设计教程》夏宇闻 第三版思考题答案(第十二章)
宝塔面板搭建小说CMS管理系统源码实测 - ThinkPHP6.0
C语言初级—从键盘接收一个整形并逆序输出
加减法运算及其溢出处理
CTF-XSS
Flask-SQLAlchemy
8576 Basic operations of sequential linear tables
C语言一级指针(补)
Flask framework in-depth
MySQL知识总结 (八) InnoDB的MVCC实现机制
MongoDB安装流程心得:
mysql常用函数
St. Regis Takeaway Notes - Lecture 05 Getting Started with Redis
The IDEA of packaged jar package
什么是 Web 3.0:面向未来的去中心化互联网
鼠标右键菜单栏太长如何减少
C语言日记 1“Hello world“
[ROS] (02) Create & compile ROS package Package
PostgreSQL 性能谜题









