Connect to MySQL (maven)

1. Add dependencies in maven's pom.xml file

 <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>8.0.11</version>
     </dependency>
复制代码

2. New properties file to save connection information

url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false
username=root
password=
复制代码

3. Create tools in the utils package

import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

public class MysqlUtil {
    /**
     * 创建数据库连接
     * @return
     * con 数据库连接
     */
    public static Connection getConnection() {
        Connection con=null;
        try {
            //加载属性文件,读取数据库连接配置信息
            Properties pro = new Properties();
            try {
                InputStream in=MysqlUtil.class.getClassLoader().getResourceAsStream("mysql.properties");
                pro.load(in);
            } catch (IOException e) {
                System.out.println("未找到配置文件!!!");
            }
            String url = pro.getProperty("url");
            String username = pro.getProperty("username");
            String password = pro.getProperty("password");
            Class.forName("com.mysql.jdbc.Driver");
            con = DriverManager.getConnection(url, username, password);// 创建数据连接
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("数据库连接失败");
        } catch (ClassNotFoundException e) {
            throw new RuntimeException("驱动类找不到");
        }
        return con;
    }


    /**
     * 关闭数据库
     * @param con
     * @param stm
     * @param rs
     */
    public static void close(Connection con, Statement stm, ResultSet rs) {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (stm != null) {
            try {
                stm.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (con != null) {
            try {
                con.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}

复制代码

4. Create a test library and write a test program in the database

import java.sql.*;

public class test_sql {
    public static void main(String[] args) {
        Connection con = null;
        Statement stm = null;
        ResultSet rs = null;
        try {
            con = MysqlUtil.getConnection();
            String sql = "SELECT * FROM test_user";
            stm = con.createStatement();
            rs = stm.executeQuery(sql);
            System.out.println("查询结果:");
            while (rs.next()) {
                String name = rs.getString("name");
                System.out.println(name);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            MysqlUtil.close(con, stm, rs);
        }
    }
    }
复制代码

5. Operation result

12312.png

6. Summary

Many variables in the database configuration file are often changed. The use of the properties file is also for the convenience of the user, allowing the user to modify the relevant variable settings without the program itself. At the same time, the database connection is made into a tool class, which is convenient for later reference and simplifies the code.

Guess you like

Origin juejin.im/post/5e9aeda1e51d4546f63099f7