MySQL > select * from ‘MySQL’;
MySQL 8.0 oR later driver package version mysql-connector-java-8.0.16.jar
2. Replace com.mysql.jdbc.Driver with com.mysql.cj.jdbc.driver.
MySQL 8.0 or later does not need to establish an SSL connection.
AllowPublicKeyRetrieval =true Allows a client to obtain a public key from the server.
Finally, you need to set up the CST.
If there is more intuitive understanding of electron, you can also refer to its official website:
www.sangpi.com
Load the driver and connect to the pager database as follows:
Class.forName(“com.mysql.cj.jdbc.Driver”); conn = DriverManager.getConnection(“jdbc:mysql://localhost:3306/test_demo? useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC”,”root”,”password”); package com.runoob.test;
import java.sql.*; WWW fhadmin org public class MySQLDemo {
Static final String JDBC_DRIVER = “com.mysql.jdbc.driver “; static final String DB_URL = “jdbc:mysql://localhost:3306/RUNOOB”;
Static final String JDBC_DRIVER = “com.mysql.cj.jdbc.driver “; static final String JDBC_DRIVER =” com.mysql.cj.jdbc.driver “; //static final String DB_URL = “jdbc:mysql://localhost:3306/RUNOOB? useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC”;
// Set the database USER name and password according to your own Settings. Static final String USER = “root”; static final String PASS = “123456”;
public static void main(String[] args) { Connection conn = null; Statement stmt = null; Try {// Register the JDBC driver class.forname (JDBC_DRIVER);
// Open the link system.out.println (" Connect to database... ); conn = DriverManager.getConnection(DB_URL,USER,PASS); System.out.println(" Instantiate Statement object... ); stmt = conn.createStatement(); String www.sangpi.comsql; sql = "SELECT id, name, url FROM websites"; ResultSet rs = stmt.executeQuery(sql); While (rs.next()){int id = rs.getint ("id"); String name = rs.getString("name"); String url = rs.getString("url"); System.out.print("ID: "+ ID); System.out.print(", "+ name); System.out.print(", URL: "+ URL); System.out.print("\n"); } // Close rs.close(); stmt.close(); conn.close(); }catch(SQLException se){// Handle JDBC error se.printStackTrace(); }catch(Exception e){// Handle class.forname error e.printStackTrace(); }finally{// Close the resource try{if(STMT! =null) stmt.close(); }catch(SQLException se2){} try{if(conn! =null) conn.close(); }catch(SQLException se){ se.printStackTrace(); } } System.out.println("Goodbye!" );Copy the code
}}