eclipse tool Java connects to MySQL version 8.0 database

Keywords: MySQL Database JDBC Java

1, Preliminary preparation

1. First, ensure that MySQL version 8.0 database is installed on the host

2. Go to the official website to download the database connection tool. MySQL version 8.0 database requires the latest connect jar connector. Otherwise, an exception of "Could not create connection to database server - java mysql connector" will be thrown. Download link: , after entering, as shown below:


3. Introduce the jar package into the Java project,

Right click the project name, first create a folder for storing jar in the project --, right click the project file name, find properties and open it as shown below

After the above process is completed, such a directory will be added in the project directory

These are the basic preparation conditions

2, Create instance connection database

package com.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class first {
	// Database address
	private static String dbUrl = "jdbc:mysql://";
	// User name
	private static String dbUserName = "ikun";
	// Password
	private static String dbPassword = "ikun2017";
	// Driver name
	private static String jdbcName = "com.mysql.cj.jdbc.Driver";
	public static void main(String[] args) {
		try {
			System.out.println("Loading driver succeeded!");
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			System.out.println("Failed to load driver!");
		Connection con = null;
		try {
			// Get database connection
			con = DriverManager.getConnection(dbUrl, dbUserName, dbPassword);
			System.out.println("Database connection succeeded!");
			System.out.println("Database operation!");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			System.out.println("Failed to get database connection!");
		} finally {
			try {
			} catch (SQLException e) {
				// TODO Auto-generated catch block

It should be noted that MySQL 8.0 introduces security encryption mechanism, and there are some differences in address connection between MySQL 8.0 and previous versions

Previous versions:

static final String DB_URL = "jdbc:mysql://

Version 8.0:

static final String dbUrl = "jdbc:mysql://";

Where student is the name of the database, useSSL is the user's choice between true and false encryption, and servertimezone is the service time zone,

There may be errors during debugging:

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 problem is still under further study,

That's all of this





Posted by soccer022483 on Wed, 08 Jan 2020 10:10:16 -0800