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

边栏推荐
- Enterprise operation we media can't "self Hi": the content should be grounded, not advertising
- Network Security Learning (I) virtual machine
- Alibaba cloud SSL certificate
- ODBC executes stored procedure to get return value
- 《ArchSummit:珍爱微服务底层框架演进》
- JDBC 驱动升级到 Version 8.0.28 连接 MySQL 的踩坑记录
- 痞子衡嵌入式:MCUXpresso IDE下将源码制作成Lib库方法及其与IAR,MDK差异
- 芯片的功耗
- Website resources
- "Fundamentals of program design" Chapter 10 function and program structure 7-2 Hanoi Tower problem (20 points)
猜你喜欢
![Luogu p2024 [noi2001] food chain](/img/7f/6ccbc19942f0d4a153025346496834.png)
Luogu p2024 [noi2001] food chain
![洛谷 P2024 [NOI2001] 食物链](/img/7f/6ccbc19942f0d4a153025346496834.png)
洛谷 P2024 [NOI2001] 食物链

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

凸优化基础知识

Network Security Learning (IV) user and group management, NTFS

Outlook邮件创建的规则失效,可能的原因

老杜Servlet-JSP

郑慧娟:基于统一大市场的数据资产应用场景与评估方法研究

Talk about how redis handles requests

NVIDA-TensorRT部署(一)
随机推荐
IndexTree2D
Network Security Learning (IV) user and group management, NTFS
"Fundamentals of program design" Chapter 10 function and program structure 6-13 divide and conquer method to solve the gold bullion problem (20 points)
MySQL查询慢的一些分析
Burp's thinking from tracing to counteracting
聊聊 Redis 是如何进行请求处理
[cloud native] Devops (IV): integrated sonar Qube
价值驱动为商业BP转型提供核心动力——业务场景下的BP实现-商业BP分享
Cross entropy loss
理财产品可以达到百分之6的,我想要开户买理财产品
Can the income of CICC securities' new customer financial products reach 6%? How to open an account?
物联网平台返回数据解析时遇到org.json.JSONException: No value for Value怎么办
必会面试题:1.浅拷贝和深拷贝_浅拷贝
Trinitycore worldofwarcraft server - registration website
How about Minsheng futures? Is it safe?
ASP.NET Core 6.0 基于模型验证的数据验证
Okaleido tiger NFT即将登录Binance NFT平台,后市持续看好
常用在线测试工具集合
基于Verilog HDL的数字秒表
Time series data in industrial Internet of things