Drive jar Package Download: Link: https://pan.baidu.com/s/1CELkAMoDAzzclKhAa5h7lg Password: XVLX
First create a new project and drag the MySQL8.0 drive jar package into the project.
Right-click the jar package, configure the build path for the jar package, and select Add directly to the build path.
This is done after the configuration:
Next, query the Country table in MySQL's own world library with JDBC, and output the names of 239 countries
Import Java.sql.connection;import java.sql.drivermanager;import java.sql.resultset;import java.sql.Statement; public class Main {public static void main (string[] args) {String db_driver = "Com.mysql.cj.jdbc.Driver"; String Db_url = "Jdbc:mysql://localhost:3306?usessl=false&servertimezone=asia/shanghai"; String db_username = "root"; String Db_password = "123456"; try {class.forname (db_driver); Connection Connection = drivermanager.getconnection (Db_url, Db_username, Db_password); Statement Statement = Connection.createstatement (); Statement.execute ("use"); ResultSet ResultSet = Statement.executequery ("SELECT * from ' Country '"), while (Resultset.next ()) {System.out.println ( Resultset.getstring ("Name"));} Resultset.close (); Statement.close (); Connection.close ();} catch (Exception e) {e.printstacktrace ();}}}
It is important to note that the MySQL8.0 and the lower versions are different: Db_driver to write Com.mysql.cj.jdbc.Driver, and to add UseSSL and Servertimezone after Db_url
JDBC Connection for MySQL8.0 under Eclipse