当前位置:网站首页>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

边栏推荐
- Ranking of engineering project management software
- Monotonic stack structure exercise -- cumulative sum of minimum values of subarrays
- Network Security Learning (II) IP address
- What is a video content recommendation engine?
- PGPLSQL中的:=和=
- 中金证券新客理财产品收益可以达到百分之六?开户怎么开?
- Process / thread synchronization mechanism
- WPF opens external programs and activates them when needed
- 高阶产品如何提出有效解决方案?(1方法论+2案例+1清单)
- Piziheng embedded: the method of making source code into lib Library under MCU Xpress IDE and its difference with IAR and MDK
猜你喜欢

痞子衡嵌入式:MCUXpresso IDE下将源码制作成Lib库方法及其与IAR,MDK差异

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

百度网盘+Chrom插件

聊聊 Redis 是如何进行请求处理

谢振东:公共交通行业数字化转型升级的探索与实践

Ranking of engineering project management software

Some analysis of slow MySQL query

burp从溯源到反制思路

Org.json Jsonexception: what about no value for value

Morris traversal
随机推荐
【云原生之kubernetes】kubernetes集群高级资源对象statefulesets
Org.json Jsonexception: what about no value for value
Learn AI through detectron2
认识复杂度和简单排序运算
PHP get thumbnails
常用在线测试工具集合
Ranking of engineering project management software
头脑风暴之——利用reduce方法重构concat函数
阿里云SSL证书
"Fundamentals of program design" Chapter 10 function and program structure 7-3 recursive realization of reverse order output integer (15 points)
凸优化基础知识
About the word 'don't remember'
Convert a string to an integer and don't double it
Let‘s Encrypt
Convex optimization Basics
QT6 with vs Code: compiling source code and basic configuration
Update structure of maximum or minimum value in the window - maximum value in the window
Collection of common online testing tools
背景图和二维码合成
先工程实践,还是先工程思想?—— 一位本科生从学oi到学开发的感悟