MySQL learning & JDBC connection MySQL(1)

MySQL learning & JDBC connection MySQL(1)

Step 1: Download and install MySQL

MySQL download address

Insert picture description here
Click, go in, you will see:
Insert picture description here

Then you are finished downloading. After downloading, it is a compressed package. After decompression, you get:
Insert picture description here

At this time, you need to configure manually. Create a file in this directory and enter the following:
[mysqld]
# 设置3306端口
port=3306
# 设置mysql的安装目录
basedir=#你安装的目录
# 设置mysql数据库的数据的存放目录
datadir=#数据库存放的目录
# 允许最大连接数
max_connections=200
# 允许连接失败的次数。
max_connect_errors=10
# 服务端使用的字符集默认为utf8mb4
character-set-server=utf8mb4
# 创建新表时将使用的默认存储引擎
default-storage-engine=INNODB
# 默认使用“mysql_native_password”插件认证
#mysql_native_password
default_authentication_plugin=mysql_native_password
[mysql]
# 设置mysql客户端默认字符集
default-character-set=utf8mb4
[client]
# 设置mysql客户端连接服务端时默认使用的端口
port=3306
default-character-set=utf8mb4
Note that the above installation directory needs to be filled in according to your installation location!
Also go to the cmd command box to install manually: (take Windows as an example)

Insert picture description here

Go to change password

Then you have to remember to change the password, because the previous operation you will get a temporary password, this MySQL8 is more advanced, the way to change the password may be different from other versions or other databases (similar to Oracle): the
command is as follows:

mysql -u root -p

alter user 用户名 identified by'新密码';

After a long configuration, I completed the MySQL installation and configuration, as well as the import of the JDBC basic package.
Then I have successfully imported the package required by JDBC, and now I am going to create the database I want under MySQL! And test the package imported by JDBC!

The second step is to configure JDBC:

Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Then go to Eclipse to configure:
Insert picture description here
go to create a new project, then Build Path, then click config,
Insert picture description here
add it and you are done!

The third step: code test JDBC connection MySQL:

First create a new database of your own:

Insert picture description here

Then connect to the database:

import java.sql.*;

public class ConnectTest {
    
    
	public static void main(String[] args) {
    
    
		Connection conn = null;
		try
		{
    
    
			Class.forName("com.mysql.cj.jdbc.Driver");
			System.out.println("Registered success!");
			
			conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/你的数据库名称?useSSL=false&serverTimezone=UTC", "用户名", "密码");
			System.out.println("Connection success!");
			conn.close();
		}
		catch(Exception e)
		{
    
    
			System.out.println("Error!");
		}
	}
}

It should be noted here:
getConnection(url, user, password) These three parameters must be filled in according to the prescribed format!
In this way, check the output content to know whether the connection to the database is complete:
Insert picture description here

Written at the back:

To do a project is really inseparable from the backend, so it must be inseparable from the database. I hope that a good project can be made and the scientific research project can be implemented!

Guess you like

Origin blog.csdn.net/qq_44274276/article/details/105555071