当前位置:网站首页>Upgrade the jdbc driver to version 8.0.28 and connect the pit record of MySQL
Upgrade the jdbc driver to version 8.0.28 and connect the pit record of MySQL
2022-04-21 13:19:00 【Siege lion Jason】
Author URI : Jason's blog
Abstract of this article : Upgrade driver to mysql-connector-java 8.0.28 Precautions for
If this article is helpful to you , Please also point your praise and collection comments to support Jason ️

List of articles
Problem description
Upgrade driver to mysql-connector-java 8.0.28 after , Deploy and execute various error reports , But switch the connector to mysql-connector-java-5.1.48 No problem , It's very confusing !
Most of the information reported in error is that such information cannot be found 、 Unable to connect
It is mainly configured , There was no screenshot , Just pay attention to the differences from the old version

Through all kinds of difficulties and dangers , Finally solved , Successfully connected , The solution is given below
Solution
The first is an example of a rookie , More comprehensive ; If you feel troublesome , The second option can be used
1. Full version
Here quote Novice tutorial Example
1. Database environment construction
-- Building a database demo1
CREATE DATABASE IF NOT EXISTS demo1;
-- establish websites surface
CREATE TABLE websites (
id int(11) NOT NULL AUTO_INCREMENT,
name char(20) NOT NULL DEFAULT '' COMMENT ' Site name ',
url varchar(255) NOT NULL DEFAULT '',
alexa int(11) NOT NULL DEFAULT '0' COMMENT 'Alexa ranking ',
country char(10) NOT NULL DEFAULT '' COMMENT ' Country ',
PRIMARY KEY (id)
) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8;
-- Write data
INSERT INTO websites
VALUES ('1', 'Google', 'https://www.google.cm/', '1', 'USA'),
('2', ' TaoBao ', 'https://www.taobao.com/', '13', 'CN'),
('3', ' Novice tutorial ', 'http://www.runoob.com', '5892', ''),
('4', ' Microblogging ', 'http://weibo.com/', '20', 'CN'),
('5', 'Facebook', 'https://www.facebook.com/', '3', 'USA');
2. Test class connection
The most important thing is here MySQL Version problem , The new version updates the name of the driver class to
com.mysql.cj.jdbc.Driver
import java.sql.*;
public class JDBCTest {
// MySQL 8.0 The following versions - JDBC Driver name and database URL
// static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
// static final String DB_URL = "jdbc:mysql://localhost:3306/RUNOOB";
// MySQL 8.0 Above version - JDBC Driver name and database 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";
// Pay attention to modifying the database name
// User name and password of database , It needs to be based on your own settings
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 {
// register JDBC drive
Class.forName(JDBC_DRIVER);
// Open the link
System.out.println(" Connect to database ...");
conn = DriverManager.getConnection(DB_URL, USER, PASS);
// Execute the query
System.out.println(" Instantiation Statement object ...");
stmt = conn.createStatement();
String sql;
sql = "SELECT id, name, url FROM websites";
ResultSet rs = stmt.executeQuery(sql);
// Expand the result set database
while (rs.next()) {
// Retrieve... By fields
int id = rs.getInt("id");
String name = rs.getString("name");
String url = rs.getString("url");
// Output data
System.out.print("ID: " + id);
System.out.print(", Site name : " + name);
System.out.print(", Site URL: " + url);
System.out.print("\n");
}
// Close... When done
rs.close();
stmt.close();
conn.close();
} catch (SQLException se) {
// Handle JDBC error
se.printStackTrace();
} catch (Exception e) {
// Handle Class.forName error
e.printStackTrace();
} finally {
// close resource
try {
if (stmt != null) stmt.close();
} catch (SQLException se2) {
}// Don't do anything?
try {
if (conn != null) conn.close();
} catch (SQLException se) {
se.printStackTrace();
}
}
System.out.println("Goodbye!");
}
}
Execution results , Successful connection

2. Lite version
The above example considers the overall , Use more exceptions , For ease of understanding , Let's simplify the amount of code
1. Database environment construction
-- Building a database demo1
CREATE DATABASE IF NOT EXISTS demo1;
-- Create data tables accounts
CREATE TABLE accounts (
id int(3) NOT NULL PRIMARY KEY auto_increment,
name varchar(5),
money FLOAT(4,2)
);
-- Write data
INSERT INTO accounts VALUES('1','jason','10000'),('2','you','99999');
2. Test class connection
Note that version 、 The order in which resources are released ( Call first , Final release , The release order is opposite to the call order )
package com.jason.jdbc;
import java.sql.*;
public class JDBCDemo {
public static void main(String[] args) throws Exception { //psvm Quickly generate
//1. Registration drive
Class.forName("com.mysql.cj.jdbc.Driver");
//2. Get the connection
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. Definition sql
String sql = "update accounts set money = 1000 where id = 2";
//4. Access to perform sql The object of Statement
Statement stmt = conn.createStatement();
//5. perform sql
int count = stmt.executeUpdate(sql);// Rows affected
//6. Processing results
System.out.println("Affected rows: "+count);
//7. Release resources Statement and Connection Pay attention to the release sequence
stmt.close();
conn.close();
}
}
Execution results , Successful connection

summary
After all, it's an update , There will be some changes , We need to learn to see what's updated 、 Learn to go to the authorities to find solutions
For example, this error report can be from the latest jar Found in the package

stay 5.x After the version , Registration driver code can be omitted without writing , This is the passage
//1. Registration drive
//Class.forName("com.mysql.cj.jdbc.Driver");
as a result of : drive jar It's a bag , Default META-INF services The corresponding driver class name is recorded in the directory , No need to write again

Maybe the version I use is too old , Can't keep up with the development of the times ~~

版权声明
本文为[Siege lion Jason]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204211316228497.html
边栏推荐
- [UVM] multi module env structure
- Selective sorting method
- 【数字信号处理】相关系数 ( 相关系数概念 | 能量信号与功率信号 | 系统的因果性 )
- Introduction to Revit secondary development -- program debugging with hellorexport (phase IV)
- SSM高校实验室安全培训系统设计与实现.docx
- Installing and configuring canal
- [digital signal processing] linear constant coefficient difference equation (determine whether the system is a "linear time invariant system" according to "linear constant coefficient difference equat
- Revit二次开发——创建标高(第八期)
- (recommended intensive reading) this refers to the north
- How a person makes self media videos, and the operation skills in the field of agriculture, rural areas and farmers
猜你喜欢
![[csnote] DB exception (redundant data, modification exception, deletion exception, insertion exception)](/img/28/adf0221ed9b25e5f0c3229441f30a2.png)
[csnote] DB exception (redundant data, modification exception, deletion exception, insertion exception)

Elements of network

Technology giants compete to enter, who can become the "number one player" of the meta universe?

(建议精读)this指北

36 day assault Tencent finally took the offer! Redis, high concurrency

网易数帆王佰平:我的 Envoy Maintainer 之路

Redis data persistence

Installing and configuring canal

Initial response Kit

Filter and listener listeners
随机推荐
焦作市第三人民医院携手美创,开启数据安全建设新局面
Q: How to change the number of appendix in the paper with the text.
Wanzi dry goods! Help you deeply master the knowledge points of "light and shadow" in design (Part 2)
Série de fenêtres coulissantes - recherche d'une chaîne de couverture minimale
动手篇 | 如何在麒麟v10 SP2安装达梦8版本数据库
PostgreSQL 15即将支持SQL标准中的MERGE语句
数字IC入门工具大全之 英特尔 Quartus Prime是什么?三个版本有什么区别
【源码解析】Encoding in Style: a StyleGAN Encoder for Image-to-Image Translation
【数字信号处理】相关函数 ( 能量信号 | 能量信号的互相关函数 | 能量信号的自相关函数 )
2021-08-10
2021-08-10
Go语言 文件操作
The second time Revit development elements are obtained
通过Ffmpeg把M3u8格式转换成Mp4
Revit secondary development - contact filtration (phase 17)
leetcode:824. Goat Latin [simple string manipulation]
This is a small case of secondary development of phase I Revit (automatic layout of supports and hangers)
万字干货!帮你深度掌握设计中的「光影」知识点
S TYLE N E RF: A S TYLE - BASED 3D-A WARE G ENERA - TOR FOR H IGH - RESOLUTION I MAGE S YNTHESIS
百度地图开发自定义信息窗口openInfoWindow样式