Connect to MySQL Database in IntelliJ IDEA with Java in Ubuntu20.04
Skill
2022-12-17 981字

Download JDBC

Download Link:MySQL :: Download Connector/J

Select Operating System:Platform Independent

Click No thanks, just start my download.

image

Extract files and place files in a memorable location.

Create new project in IDEA

1、Create new project.

2、Add a directory.

image

3、Select lib -> right click -> Opin in -> files -> Import JDBC

image

Add it as a module

Select testJDBC -> right click -> Project Structure -> Module -> JAR

image

Create Module in MySQL Workbench

image

Editor Java Code

In src/Main.java

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) {
        try {
            Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/book",
                    "root", "11");
            Statement statement = connection.createStatement();

            ResultSet resultSet = statement.executeQuery("select * from advisor");
            while (resultSet.next()) {
                System.out.println(resultSet.getString("s_ID"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

image

参考链接

视频参考:Java JDBC - Connect to MySQL Database in IntelliJ with Java - YouTube