JDBC连接mysql8版本遇到的问题

1.首先下载合适的驱动包,8版本所用驱动jar包和5版本有出入,下载8版本地址如下:

链接:https://pan.baidu.com/s/1YJudxw01tG9PGBVxMZntkg
提取码:jbml

2.在eclipse 新建(java工程)java project,并导入第一步下载的jar包,新建一个类如JdbcDemo如图:
image.png

3.编写JdbcDemo 程序

具体实现步骤如下:

  1. 注册驱动
    Class.forName("com.mysql.cj.jdbc.Driver").newInstance();

2.创建和数据库的连接对象:是Connection接口的实现类对象
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/testJdbc?" + "user=root&password=123456&useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false");
3.获取Sql语句的执行者对象,是Statement接口的实现类对象
Statement stmt = conn.createStatement();
4.获取结果集对象,是ResultSet接口的实现类对象
ResultSet rs = stmt.executeQuery("select* from user");
5.处理结果集

while (rs.next()) {
  Object id = rs.getObject("id");
  Object username = rs.getObject("username");
  System.out.println(id+"==="+username);
}

6.释放资源

rs.close();
stmt.close();
conn.close();

整个示例代码如下:

package com.liquan;

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

public class JdbcDemo {
    public static void main(String[] args){
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;
         try {
            Class.forName("com.mysql.cj.jdbc.Driver").newInstance();
             conn =
                       DriverManager.getConnection("jdbc:mysql://localhost/testJdbc?" +
                                                   "user=root&password=123456&useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false");
             stmt = conn.createStatement();
                rs = stmt.executeQuery("SELECT * FROM user");
                while(rs.next()){
                      Object id = rs.getObject("id");
                              Object username = rs.getObject("username");
                              System.out.println(id+"==="+username);
                    }

        } catch (InstantiationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            // it is a good idea to release
            // resources in a finally{} block
            // in reverse-order of their creation
            // if they are no-longer needed

            if (rs != null) {
                try {
                    rs.close();
                } catch (SQLException sqlEx) { } // ignore

                rs = null;
            }

            if (stmt != null) {
                try {
                    stmt.close();
                } catch (SQLException sqlEx) { } // ignore

                stmt = null;
            }
        }
    }
}

ps: 使用的过程中可能会出现连接错误等,有可能的原因是mysql8默认开启了安全认证,或者是因为时区的原因造成的,因此创建连接的时候可以制定一些连接参数如:
useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false

你可能感兴趣的:(JDBC连接mysql8版本遇到的问题)