当前位置:网站首页>Upgrade the jdbc driver to version 8.0.28 and connect to the pit record of MySQL
Upgrade the jdbc driver to version 8.0.28 and connect to the pit record of MySQL
2022-07-24 22:56:00 【InfoQ】
Problem description
mysql-connector-java 8.0.28mysql-connector-java-5.1.48
Solution
1. Full version
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
com.mysql.cj.jdbc.Driverimport 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!");
}
}

2. Lite version
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
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();
}
}

summary

5.x//1. Registration drive
//Class.forName("com.mysql.cj.jdbc.Driver");
jarMETA-INF services

边栏推荐
- Flex layout
- "Fundamentals of program design" Chapter 10 function and program structure 7-3 recursive realization of reverse order output integer (15 points)
- 把字符串转换成整数与不要二
- Network Security Learning (V) DHCP
- 价值驱动为商业BP转型提供核心动力——业务场景下的BP实现-商业BP分享
- Convert a string to an integer and don't double it
- Monotonic stack structure
- ShardingSphere-数据库分库分表简介
- QT6 with vs Code: compiling source code and basic configuration
- yolov5
猜你喜欢
![洛谷 P2024 [NOI2001] 食物链](/img/7f/6ccbc19942f0d4a153025346496834.png)
洛谷 P2024 [NOI2001] 食物链

Notes of Teacher Li Hongyi's 2020 in-depth learning series lecture 1

Network Security Learning (V) DHCP

Talk about how redis handles requests

CA证书制作实战

Introduction to HLS programming

背景图和二维码合成

JDBC 驱动升级到 Version 8.0.28 连接 MySQL 的踩坑记录

burp从溯源到反制思路

Use kettle to read the data in Excel file and store it in MySQL
随机推荐
Segment tree,,
Some analysis of slow MySQL query
必会面试题:1.浅拷贝和深拷贝_深拷贝
QT6 with vs Code: compiling source code and basic configuration
Financial products can reach 6%. I want to open an account to buy financial products
WPF opens external programs and activates them when needed
ASP.NET Core 6.0 基于模型验证的数据验证
"Fundamentals of program design" Chapter 10 function and program structure 7-2 Hanoi Tower problem (20 points)
Pointrender parsing
Org.json Jsonexception: what about no value for value
[cloud native kubernetes] kubernetes cluster advanced resource object staterulesets
芯片的功耗
Is the income of CICC securities' new financial products 6%? I want to open an account and manage money
Violent recursion - detailed explanation of Queen n & how to optimize with bit operation
理财产品可以达到百分之6的,我想要开户买理财产品
JUC concurrent programming - Advanced 05 - lock free of shared model (CAS | atomic integer | atomic reference | atomic array | field updater | atomic accumulator | unsafe class)
About the word 'don't remember'
Filter list
关于板载继电器供电不足引起不能吸合的问题
Value driven provides the core driving force for the transformation of commercial BP - BP realization under business scenarios - Commercial BP sharing