当前位置:网站首页>JDBC 驱动升级到 Version 8.0.28 连接 MySQL 的踩坑记录
JDBC 驱动升级到 Version 8.0.28 连接 MySQL 的踩坑记录
2022-07-24 22:37:00 【InfoQ】
问题描述
mysql-connector-java 8.0.28mysql-connector-java-5.1.48
解决方案
1.完整版
1.数据库环境搭建
-- 建立数据库 demo1
CREATE DATABASE IF NOT EXISTS demo1;
-- 建立 websites 表
CREATE TABLE websites (
id int(11) NOT NULL AUTO_INCREMENT,
name char(20) NOT NULL DEFAULT '' COMMENT '站点名称',
url varchar(255) NOT NULL DEFAULT '',
alexa int(11) NOT NULL DEFAULT '0' COMMENT 'Alexa 排名',
country char(10) NOT NULL DEFAULT '' COMMENT '国家',
PRIMARY KEY (id)
) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8;
-- 写入数据
INSERT INTO websites
VALUES ('1', 'Google', 'https://www.google.cm/', '1', 'USA'),
('2', '淘宝', 'https://www.taobao.com/', '13', 'CN'),
('3', '菜鸟教程', 'http://www.runoob.com', '5892', ''),
('4', '微博', 'http://weibo.com/', '20', 'CN'),
('5', 'Facebook', 'https://www.facebook.com/', '3', 'USA');
2.测试类连接
com.mysql.cj.jdbc.Driverimport java.sql.*;
public class JDBCTest {
// MySQL 8.0 以下版本 - JDBC 驱动名及数据库 URL
// static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
// static final String DB_URL = "jdbc:mysql://localhost:3306/RUNOOB";
// MySQL 8.0 以上版本 - JDBC 驱动名及数据库 URL
static final String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost:3306/demo1?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC&useServerPrepStmts=true";
// 注意修改数据库名
// 数据库的用户名与密码,需要根据自己的设置
static final String USER = "your db login name";
static final String PASS = "your db password";
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
try {
// 注册 JDBC 驱动
Class.forName(JDBC_DRIVER);
// 打开链接
System.out.println("连接数据库...");
conn = DriverManager.getConnection(DB_URL, USER, PASS);
// 执行查询
System.out.println(" 实例化Statement对象...");
stmt = conn.createStatement();
String sql;
sql = "SELECT id, name, url FROM websites";
ResultSet rs = stmt.executeQuery(sql);
// 展开结果集数据库
while (rs.next()) {
// 通过字段检索
int id = rs.getInt("id");
String name = rs.getString("name");
String url = rs.getString("url");
// 输出数据
System.out.print("ID: " + id);
System.out.print(", 站点名称: " + name);
System.out.print(", 站点 URL: " + url);
System.out.print("\n");
}
// 完成后关闭
rs.close();
stmt.close();
conn.close();
} catch (SQLException se) {
// 处理 JDBC 错误
se.printStackTrace();
} catch (Exception e) {
// 处理 Class.forName 错误
e.printStackTrace();
} finally {
// 关闭资源
try {
if (stmt != null) stmt.close();
} catch (SQLException se2) {
}// 什么都不做
try {
if (conn != null) conn.close();
} catch (SQLException se) {
se.printStackTrace();
}
}
System.out.println("Goodbye!");
}
}

2.精简版
1.数据库环境搭建
-- 建立数据库 demo1
CREATE DATABASE IF NOT EXISTS demo1;
-- 建立数据表 accounts
CREATE TABLE accounts (
id int(3) NOT NULL PRIMARY KEY auto_increment,
name varchar(5),
money FLOAT(4,2)
);
-- 写入数据
INSERT INTO accounts VALUES('1','jason','10000'),('2','you','99999');
2.测试类连接
package com.jason.jdbc;
import java.sql.*;
public class JDBCDemo {
public static void main(String[] args) throws Exception { //psvm 快速生成
//1. 注册驱动
Class.forName("com.mysql.cj.jdbc.Driver");
//2. 获取连接
String url = "jdbc:mysql://localhost:3306/demo1?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC&useServerPrepStmts=true";
String username = "your db login name";
String password = "your db password";
Connection conn = DriverManager.getConnection(url, username, password);
//3. 定义sql
String sql = "update accounts set money = 1000 where id = 2";
//4. 获取执行sql的对象 Statement
Statement stmt = conn.createStatement();
//5. 执行sql
int count = stmt.executeUpdate(sql);//受影响的行数
//6. 处理结果
System.out.println("Affected rows: "+count);
//7. 释放资源 Statement 和 Connection 注意释放顺序
stmt.close();
conn.close();
}
}

总结

5.x//1. 注册驱动
//Class.forName("com.mysql.cj.jdbc.Driver");
jarMETA-INF services

边栏推荐
- 【云原生之kubernetes】kubernetes集群高级资源对象statefulesets
- 阿里云SSL证书
- The rule created by outlook mail is invalid. Possible reasons
- Okaleido tiger NFT is about to log in to binance NFT platform, and the future market continues to be optimistic
- 解决JSP无法使用session.getAttribute()
- 价值驱动为商业BP转型提供核心动力——业务场景下的BP实现-商业BP分享
- 聊聊 Redis 是如何进行请求处理
- 从暴力递归到动态规划,记忆化搜索
- Available parameters of ansible Playbook
- Update structure of maximum or minimum value in the window - maximum value in the window
猜你喜欢

AC automata

图结构的实现,从点到边再到图

Monotonic stack structure exercise -- cumulative sum of minimum values of subarrays

JUC concurrent programming - Advanced 05 - lock free of shared model (CAS | atomic integer | atomic reference | atomic array | field updater | atomic accumulator | unsafe class)

burp从溯源到反制思路

认识复杂度和简单排序运算
WPF uses pathgeometry to draw the hour hand and minute hand

The specified data is grouped and the number of repetitions is obtained in Oracle

Talk about how redis handles requests

生成式对抗网络的效果评估
随机推荐
并查集结构
JUC concurrent programming - Advanced 05 - lock free of shared model (CAS | atomic integer | atomic reference | atomic array | field updater | atomic accumulator | unsafe class)
IndexTree
P3201 [HNOI2009] 梦幻布丁 启发式合并
VScode默认输出到调试控制台如何调整到终端以及两者中的乱码问题
头脑风暴之——利用reduce方法重构concat函数
Connector in C
SQL语言的通用语法及分类(二)
生成式对抗网络的效果评估
Monotonic stack structure exercise -- cumulative sum of minimum values of subarrays
工业物联网中的时序数据
Moving least squares fitting experiment of PCL point cloud processing (62)
MySQL查询慢的一些分析
IndexTree2D
A compatible, smaller and easy-to-use web font API
Joint search set structure
【零基础】php代码审计之sql注入
The tragic experience of installing scikitlearn on win764
Read and understand the advantages of the LAAS scheme of elephant swap
From violent recursion to dynamic programming, memory search