You do not know --JDBC connect to the MySQL driver bitterness story

How to download drivers

Recommended Reading

Download Link

Note If WindowsOS not directly under Windows, that is not JDBC, you should choose the above "Selecting Operating System ...", then select "Plantform Independent", where the election that .zip file. Select the above Unix / Linux the .tar.gz file.

Package is small, so do not need to download all kinds of weird stuff, do not go CSDN download area is cut chives, and that they can download, endured a little slow.

There are open .zip .jar, self-created and import the project.

  • The .jar imported idea, then recommend reading this article .
  • The .jar into eclipse, it is recommended to read this article .

解读 jdbc:mysql://localhost:3306/[database_name]

  • jdbc: MySQL: // : JDBC Connection
  • localhost : local address
  • 3306 : The port number SQL database (MySQL use this on the line)
  • [database_name] : the name of the database to be connected

In general, you only need to write their own look DatabaseName can pay attention to this library should exist, otherwise it will error.

Processing "com.mysql.jdbc.Driver" error

Because I use MySQL 8.0.16 , JAR package is MySQL-Connector-the Java-8.0.19.jar , so the version is relatively high, it should be used: com.mysql.cj.jdbc.Driver .

Deal with the garbage problem

There may also be up and running garbage problem, I encountered a garbled exception stack problem is this:

java.sql.SQLException: The server time zone value ‘�й���׼ʱ��’ is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the ‘serverTimezone’ configuration property) to use a more specifc time zone value if you want to utilize time zone support.
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:129)
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97)
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:89)
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:63)
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:73)
    at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:76)
    at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:836)
    at com.mysql.cj.jdbc.ConnectionImpl.(ConnectionImpl.java:456)
    at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:246)
    at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:197)
    at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:677)
    at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:228)
    at main.JDBCTest.main(JDBCTest.java:19)
Caused by: com.mysql.cj.exceptions.InvalidConnectionAttributeException: The server time zone value ‘�й���׼ʱ��’ is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the ‘serverTimezone’ configuration property) to use a more specifc time zone value if you want to utilize time zone support.
  at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
  at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
  at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
  at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490)
  at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:61)
  at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:85)
  at com.mysql.cj.util.TimeUtil.getCanonicalTimezone(TimeUtil.java:132)
  at com.mysql.cj.protocol.a.NativeProtocol.configureTimezone(NativeProtocol.java:2118)
  at com.mysql.cj.protocol.a.NativeProtocol.initServerSession(NativeProtocol.java:2142)
  at com.mysql.cj.jdbc.ConnectionImpl.initializePropsFromServer(ConnectionImpl.java:1310)
  at com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:967)
  at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:826)
  … 6 more

How to handle it? Recommended reading this article

You can run code

Database name, user name, password, these three need their own replacement, pay attention to whether the version is probably the same with me, if you are 5+ version is very different!

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class JDBCTest {
    public static void main(String[] args) {
        String driverName="com.mysql.cj.jdbc.Driver";
        String dbURL="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai";
        // 你的登录名,自己写,比如root
        String userName="userName";
        // 你的登录密码,自己写
        String userPassword="userPassword";
        try{
            Class.forName(driverName);
            System.out.println("加载MySQL驱动成功");
        } catch(ClassNotFoundException e) {
            System.out.println("加载MySQL驱动失败");
        }
        try (Connection dbConnection = DriverManager.getConnection(dbURL, userName, userPassword)) {
            System.out.println("连接数据库成功");
        } catch(SQLException e) {
            System.out.println("数据库连接失败");
        }
    }
}

operation result

加载MySQL驱动成功
连接数据库成功
Published 652 original articles · won praise 1340 · Views 580,000 +

Guess you like

Origin blog.csdn.net/weixin_43896318/article/details/104706238