你所不知道的——JDBC连接MySQL驱动的辛酸故事

如何下载驱动

推荐阅读

下载链接

注意如果是WindowsOS不要直接下Windows,那个不是JDBC,应该选上面的"Selecting Operating System…",再选"Plantform Independent",在这里选那个.zip文件。Unix/Linux的选择上面的.tar.gz文件。

包很小,所以下载不需要各种奇奇怪怪的东西,也不必去CSDN下载区被割韭菜,自己就可以下载,慢就忍着点儿。

打开.zip就有.jar,自取并导入工程。

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

  • jdbc:mysql://:JDBC连接方式
  • localhost:本机地址
  • 3306:SQL数据库的端口号(MySQL用这个就行了)
  • [database_name]:待连接的数据库的名称

一般来说,只需要自己写一下DatabaseName即可,注意这个库应该是存在的,否则会报错。

处理"com.mysql.jdbc.Driver"的错误

由于我用的是 MySQL 8.0.16,jar包是 mysql-connector-java-8.0.19.jar ,所以版本比较高,应该使用的是:com.mysql.cj.jdbc.Driver

处理乱码问题

运行起来可能还会有乱码问题,我遇到过的一个乱码问题的异常栈是这样的:

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

如何处理呢?推荐阅读这篇文章

可运行代码

数据库名、用户名、密码 这三个需要自己替换,注意版本是否与我大概一致,如果是5+版本就很不一样!

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("数据库连接失败");
        }
    }
}

运行结果

加载MySQL驱动成功
连接数据库成功
发布了652 篇原创文章 · 获赞 1340 · 访问量 58万+

猜你喜欢

转载自blog.csdn.net/weixin_43896318/article/details/104706238