Java JDBC connection mysql and IDEA configuration

java DateBase Connectiveity Write sql in java.

The java.sql.* packages are all JDBC interfaces, which are made by the Sun company.

Six steps:

(1) Registering the driver
(2) Obtaining database connection
(3) Obtaining database operation objects (executing SQL statements)
(4) Executing SQL statements (crud operations)
(5) Processing query results
(6) Releasing resources

Register the driver

The following is the configuration in IDEA2021:

(1) Create an empty java project, right-click on the project folder, select Open Module Settings,

(2) Click Libraries, the specific operation is as follows:

(3) After clicking java, a window will pop up and find the local mysql-connector-java-xx.xx.xx-bin.jar package (the version of this jar package depends on the version of Mysql on your computer. If mysql is 8.xx. You need to download the 8.xx jar package. Download my jar package here, extraction code-rv77 ), select it, and click OK

(4) The following display will appear at this time:

(5) Click Apply -> OK.

Link to Mysql

After completing the basic operations above, you can link to the database below. Take the mysql database as an example below:

package com;import java.sql.*; // 需要导包/** *  协议:通信协议是通信之前就定好的数据传输格式,数据包具体怎么传输数据,格式提前定好的 * jdbc:mysql:// * 127.0.0.1  ip地址 * 3306    端口号 * java_pro    连接的数据库名称 */public class lxc {    public static void main(String[] args) {        String url = "jdbc:mysql://127.0.0.1:3306/java_pro";        String user = "root";        String password = "[email protected]";        try {            // 注册驱动            Class.forName("com.mysql.jdbc.Driver");            // 获取连接 - 连接mysql数据库            Connection conne = null;            try {                conne = DriverManager.getConnection(url, user, password); // 获取连接对象            } catch (SQLException throwables) {                throwables.printStackTrace();            }            System.out.println("连接对象"+conne); // [email protected]        } catch (ClassNotFoundException e) {            e.printStackTrace();        }    }}

The connection is successful.