Chinaunix首页 | 论坛 | 博客
  • 博客访问: 553343
  • 博文数量: 298
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 3077
  • 用 户 组: 普通用户
  • 注册时间: 2019-06-17 10:57
文章分类

全部博文(298)

文章存档

2022年(96)

2021年(201)

2019年(1)

我的朋友

分类: Java

2021-05-23 10:32:33

MySQL 8.0 以上版本的数据库连接有所不同:

  • 1、MySQL 8.0 以上版本驱动包版本 mysql-connector-java-8.0.16.jar。

  • 2、com.mysql.jdbc.Driver 更换为 com.mysql.cj.jdbc.Driver

  • MySQL 8.0 以上版本不需要建立 SSL 连接的,需要显示关闭。

  • allowPublicKeyRetrieval=true 允许客户端从服务器获取公钥。

  • 最后还需要设置 CST。

加载驱动与连接数据库方式如下:

Class.forName("com.mysql.cj.jdbc.Driver"); conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test_demo?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC","root","password");
MySQLDemo.java 文件代码:


点击(此处)折叠或打开

  1. package com.runoob.test;
  2.  
  3. import java.sql.*;
  4.  
  5. //java 项目 www fhadmin org
  6. public class MySQLDemo {
  7.  
  8.     // MySQL 8.0 以下版本 - JDBC 驱动名及数据库 URL
  9.     static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
  10.     static final String DB_URL = "jdbc:mysql://localhost:3306/RUNOOB";
  11.  
  12.     // MySQL 8.0 以上版本 - JDBC 驱动名及数据库 URL
  13.     //static final String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver";
  14.     //static final String DB_URL = "jdbc:mysql://localhost:3306/RUNOOB?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC";
  15.  
  16.  
  17.     // 数据库的用户名与密码,需要根据自己的设置
  18.     static final String USER = "root";
  19.     static final String PASS = "123456";
  20.  
  21.     public static void main(String[] args) {
  22.         Connection conn = null;
  23.         Statement stmt = null;
  24.         try{
  25.             // 注册 JDBC 驱动
  26.             Class.forName(JDBC_DRIVER);
  27.         
  28.             // 打开链接
  29.             System.out.println("连接数据库...");
  30.             conn = DriverManager.getConnection(DB_URL,USER,PASS);
  31.         
  32.             // 执行查询
  33.             System.out.println(" 实例化Statement对象...");
  34.             stmt = conn.createStatement();
  35.             String sql;
  36.             sql = "SELECT id, name, url FROM websites";
  37.             ResultSet rs = stmt.executeQuery(sql);
  38.         
  39.             // 展开结果集数据库
  40.             while(rs.next()){
  41.                 // 通过字段检索
  42.                 int id = rs.getInt("id");
  43.                 String name = rs.getString("name");
  44.                 String url = rs.getString("url");
  45.     
  46.                 // 输出数据
  47.                 System.out.print("ID: " + id);
  48.                 System.out.print(", 站点名称: " + name);
  49.                 System.out.print(", 站点 URL: " + url);
  50.                 System.out.print("\n");
  51.             }
  52.             // 完成后关闭
  53.             rs.close();
  54.             stmt.close();
  55.             conn.close();
  56.         }catch(SQLException se){
  57.             // 处理 JDBC 错误
  58.             se.printStackTrace();
  59.         }catch(Exception e){
  60.             // 处理 Class.forName 错误
  61.             e.printStackTrace();
  62.         }finally{
  63.             // 关闭资源
  64.             try{
  65.                 if(stmt!=null) stmt.close();
  66.             }catch(SQLException se2){
  67.             }// 什么都不做
  68.             try{
  69.                 if(conn!=null) conn.close();
  70.             }catch(SQLException se){
  71.                 se.printStackTrace();
  72.             }
  73.         }
  74.         System.out.println("Goodbye!");
  75.     }
  76. }




阅读(2526) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~