JAVA uses JDBC to connect to the pit of mysql database! !

JAVA uses JDBC to connect to the pit of mysql database! !

1. Source code display

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();
    }
}

This is the source code, and there is no problem at first glance. . . .

When you click to run, the scary thing begins, a bunch of errors are reported. . . .

2. Run an error! ! !

3. Analyze the error and modify the code

Let's take a look at the error message first

3.1 First:

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.

This sentence reminds us that we did not successfully load the driver class com.mysql.jdbc.Driver

Then it prompts us that the driver class should be in the com.mysql.cj.jdbc.Driver directory, then we first find this error, first change com.mysql.jdbc.Driver to com.mysql.cj.jdbc. Driver

(Version problem, if you use com.mysql.jdbc.Driver directly, there is no problem, um, it proves that there is no problem with your version...)

3.2 Error in the second paragraph

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

 The second paragraph and the third paragraph basically mean the same thing, so let’s talk about it together~~

First look at the error report, we get to a point where the server time reports an error. ? ?

Read it carefully, what it tells us is that there is a difference in the time zone between our database and the system.

How to solve this problem? ? ? Very simple

The problem can be solved by adding serverTimezone = GMT to the URL of the JDBC connection. If you need to use GMT + 8 time zone, you need to write GMT% 2B8 (2B is the hexadecimal ASCII code of "+", and "%2B" is "+" ”), otherwise it will be parsed as empty.

4. Modified code

Now correct these errors first, and the corrected code is as follows:

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();
    }
}

Run now! !

5. Running results:

Success! ! ! ! No error is reported, the effect is what we want! ! ! !

 

Guess you like

Origin blog.csdn.net/caixuanji/article/details/107202408