- Load database driver
- Create database connection
- Create operation command
- Execute SQL statement
- Process return result set
- Close result set
- Close operation command
- Close connection
First load the driver
Project right click to find Open Module Settings open
After opening, do the following
Find the driver you downloaded in the local file
Then apply, ok. That's all
Click lib under the project, as shown in the figure, to load the driver successfully
import java.sql.*; import java.time.LocalDateTime; public class TestJDBC { public static void main(String[] args) { try { //1 / load drive Class.forName("com.mysql.jdbc.Driver"); } catch (ClassNotFoundException e) { e.printStackTrace(); } //2. Connect to database //jdbc:database://host:port/databaseName?p1=v1&p2=v2 try { Connection connection = DriverManager.getConnection("jdbc:MySQL://localhost:3306/memo?useSSL=false", "root", "123456"); //3. Create command Statement statement = connection.createStatement(); //4. Prepare sql and execute ResultSet resultSet = statement.executeQuery("select id,name,created_time,modify_time from memo_group"); //5. Return result set and process result while (resultSet.next()) { //If true is returned, there is the next record line, otherwise there is no record int id = resultSet.getInt("id"); String name = resultSet.getString("name"); LocalDateTime createTime = resultSet.getTimestamp("created_time").toLocalDateTime(); LocalDateTime modifyTime=resultSet.getTimestamp("modify_time").toLocalDateTime(); System.out.println( String.format("Serial number:%d,Name:%s,Created:%s,Modified:%s",id,name,createTime.toString(),modifyTime.toString()) ); } //6. Close result set resultSet.close(); //7. Close command statement.close(); //8. Close connection connection.close(); } catch (SQLException e) { e.printStackTrace(); } } }```