No suitable driver found for jdbc:mysql:localhost:mysql when using JDBC to connect to MySQL database
MySQL version: 8.0.26
Change the driver to:
“jdbc:mysql://localhost:3306/mysql?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai”
import java.sql.*;
public class JDBCTest {
public static void main(String[] args) {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
Connection conn = null;
try {
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mysql?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai","root","dcc12345");
System.out.println("数据库连接成功");
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
if(conn != null){
try {
conn.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
}
}
The writing method of the new version of MySQL driver is different from that of the previous version. Jar package 8.0. * everything is common
Read More:
- Extracting JDBC tool class: JDBC utils
- Public Key Retrieval is not allowed [How to Solve]
- Java database Druid error: com.alibaba.druid.pool.DruidDataSource error
- [Solved] Springboot connect MySQL error: errorcode 0, state 08s01
- Solution to javax.naming.noinitialcontextexception error
- [Solved] Springboot Error: Error creating bean with name ‘dataSource‘ defined in class path resource
- [Solved] com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure error
- [Solved] Cannot find class: com.mysql.jdbc.Driver
- [Solved] Caused by: com.mysql.cj.exceptions.InvalidConnectionAttributeException
- [Solved] Mybatis Batch Modify Error: multi-statement not allow
- [Solved] java.sql.SQLException: Unsupported character encoding ‘utf-8
- Path does not chain with any of the trust anchors [Solved]
- [Solved] java.sql.SQLException: Unknown system variable cache query size
- How to Solve JDBC connection error in spring MVC integration
- [Solved] mybatis Error querying database. Cause: java.sql.SQLException: The server time zone value
- Error querying database.Cause: java.sql.SQLException: Error setting driver on UnpooledDataSource.
- Error creating bean with name ‘servletEndpointRegistrar‘ defined in class path resource
- [Solved] springboot Project Run Error: HikariPool-1 – Exception during pool initialization.
- [Solved] init datasource error, url: jdbc:mysql://localhost:3306/test
- [Solved] IDEA Error: Unknown system variable ‘tx_isolation‘ at