JAVA 使用JDBC连接mysql数据库的坑!!

JAVA 使用JDBC连接mysql数据库的坑!!

1.源代码展示

package com.xuanji.lesson01;

import java.sql.*;

//我的第一个jdbc程序
public class JDBCFirstDemo {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        //1.加载驱动
        Class.forName("com.mysql.jdbc.Driver");//固定写法,加载驱动
        //2.用户信息和url
        String url="jdbc:mysql://localhost:3306/mydb?useUnicode=true&characterEncoding=utf8&useSSL=true";
        String username="root";
        String password="******";//密码不便展示
        //3.连接成功,返回数据库对象
        Connection connection= DriverManager.getConnection(url,username,password);
        //4.执行SQL的对象
        Statement statement = connection.createStatement();
        //5.执行SQL的对象去执行SQL,可能存在结果,查看返回结果
        String sql="SELECT * FROM sp_type";
        ResultSet resultSet = statement.executeQuery(sql);//返回的结果集,结果集中封装了我们全部的查询出来的结果
        while(resultSet.next()){
            System.out.println("type_id="+resultSet.getObject("type_id"));
            System.out.println("type_name="+resultSet.getObject("type_name"));
            System.out.println("delete_time="+resultSet.getObject("delete_time"));
        }
        //6.释放连接
        resultSet.close();
        statement.close();
        connection.close();
    }
}

这个是源代码,乍一看是没有任何问题的。。。。

当你点击运行的时候,恐怖的事情就开始了,一堆报错。。。。

2.运行报错!!!

3.分析报错修改代码

我们先来看看报错的信息

3.1首先:

Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.

这一句提示我们并没能成功加载com.mysql.jdbc.Driver这个驱动类

然后它提示我们这个驱动类应该是在com.mysql.cj.jdbc.Driver这个目录下的,那我们首先找到这个错误,先把com.mysql.jdbc.Driver改为com.mysql.cj.jdbc.Driver

(版本问题,如果你直接用com.mysql.jdbc.Driver没有问题的话,嗯,就证明你的版本没有问题。。。。。)

3.2第二段报错

Exception in thread "main" 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.<init>(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.DriverManager.getConnection(DriverManager.java:664)
    at java.sql.DriverManager.getConnection(DriverManager.java:247)
    at com.xuanji.lesson01.JDBCFirstDemo.main(JDBCFirstDemo.java:15)
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 sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    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

 第二段报错跟第三段报错基本上是一个意思,就一起讲啦~~

首先看报错我们get到一个点就是server time报错,???

仔细阅读下去,它告诉我们的意思是我们的数据库和系统时区上有差异所造成的。

那要怎么解决这个问题呢???很简单

在JDBC连接的URL后面加上serverTimezone = GMT即可解决问题,如果需要使用GMT + 8时区,需要写成GMT%2B8(2B即"+“的十六进制ASCII码,”%2B"即“+”),否则会被解析为空。

4.修改后的代码

那现在把这些错误地方先改过来,改完的代码如下:

package com.xuanji.lesson01;

import java.sql.*;

//我的第一个jdbc程序
public class JDBCFirstDemo {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        //1.加载驱动
        Class.forName("com.mysql.cj.jdbc.Driver");//固定写法,加载驱动
        //2.用户信息和url
        String url="jdbc:mysql://localhost:3306/mydb?useUnicode=true&characterEncoding=utf8&useSSL=true&serverTimezone=GMT";
        String username="root";
        String password="******";//密码不便展示
        //3.连接成功,返回数据库对象
        Connection connection= DriverManager.getConnection(url,username,password);
        //4.执行SQL的对象
        Statement statement = connection.createStatement();
        //5.执行SQL的对象去执行SQL,可能存在结果,查看返回结果
        String sql="SELECT * FROM sp_type";
        ResultSet resultSet = statement.executeQuery(sql);//返回的结果集,结果集中封装了我们全部的查询出来的结果
        while(resultSet.next()){
            System.out.println("type_id="+resultSet.getObject("type_id"));
            System.out.println("type_name="+resultSet.getObject("type_name"));
            System.out.println("delete_time="+resultSet.getObject("delete_time"));
        }
        //6.释放连接
        resultSet.close();
        statement.close();
        connection.close();
    }
}

现在再来运行!!

5.运行结果:

成功啦!!!!没有报错,出来的效果也是我们要的效果啦!!!!

猜你喜欢

转载自blog.csdn.net/caixuanji/article/details/107202408