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

边栏推荐
- DDos攻击分类
- VGA display based on FPGA
- CA certificate production practice
- IndexTree2D
- [cloud native] Devops (IV): integrated sonar Qube
- Can the income of CICC securities' new customer financial products reach 6%? How to open an account?
- 用VS Code搞Qt6:编译源代码与基本配置
- Okaleido tiger NFT即将登录Binance NFT平台,后市持续看好
- ASP. Net core 6.0 data validation based on model validation
- Burp's thinking from tracing to counteracting
猜你喜欢

Network Security Learning (III) basic DOS commands

基于FPGA的VGA显示
![[database learning] redis parser & single thread & Model](/img/70/c84eb02d45e35fede4dd1b1ff04392.png)
[database learning] redis parser & single thread & Model

《元宇宙2086》单日销量超《三体》 夺得京东图书单日科幻榜冠军

HLS编程入门

Convert a string to an integer and don't double it

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

IndexTree2D

From violent recursion to dynamic programming, memory search

Baidu online disk +chrome plug-in
随机推荐
先工程实践,还是先工程思想?—— 一位本科生从学oi到学开发的感悟
CA证书制作实战
老杜Servlet-JSP
RichTextBox operation
On the open and closed principle
Introduction to HLS programming
On the problem that the on-board relay cannot be switched on due to insufficient power supply
Backgroundworker enables time-consuming operations without affecting interface response
Talk about how redis handles requests
Things to study
高阶产品如何提出有效解决方案?(1方法论+2案例+1清单)
用VS Code搞Qt6:编译源代码与基本配置
把字符串转换成整数与不要二
Network Security Learning (I) virtual machine
网上怎么炒股手机上炒股安全吗
关于板载继电器供电不足引起不能吸合的问题
Read and understand the advantages of the LAAS scheme of elephant swap
The kettle job implementation runs a kettle conversion task every 6S
GB、MB、KB分别是什么意思。大小分别是多少
Value driven provides the core driving force for the transformation of commercial BP - BP realization under business scenarios - Commercial BP sharing