1. 程式人生 > >IDEA中使用properties連線mysql8.0.13

IDEA中使用properties連線mysql8.0.13

IDEA中使用properties連線mysql8.0.13

1.IDEA下匯入mysql-connector-java-8.0.13.jar,並新增到Modules中

2.在專案下新建一個目錄(Directory),一般properties資料夾命名應為resoures

3.右鍵新建的resources資料夾,選擇Mark Dictory as >>Resources Root將資料夾定義為配置資料夾

4.右鍵resources型別的資料夾選擇Resource Bundle,就能建立properties檔案了

5.mysql8.0.13的properties檔案內容為:

注意:配置檔案是以鍵值對形式來進行存/讀取的,一個物件佔用一行,行末不能新增分號

driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/goods?useSSL=false&serverTimezone=UTC
username=root
password=123456

6.properties使用的靜態程式碼塊有兩種:

第一種:Properties

public static String driver;
    public static String url;
    public static String username;
    public static String password;
    static {
        Properties pt = new Properties();
        try{
            FileInputStream in = new FileInputStream("resources/db.properties");
            pt.load(in);
            driver = pt.getProperty("driver");
            url = pt.getProperty("url");
            username = pt.getProperty("username");
            password = pt.getProperty("password");
        }catch (IOException e){
            e.printStackTrace();
        }
    }
public static String driver;
    public static String url;
    public static String username;
    public static String password;
    static {
        try{
            ClassLoader classLoader = JDBCUtils.class.getClassLoader();
            InputStream is = classLoader.getResourceAsStream("db.properties");
            Properties pt = new Properties();
            pt.load(is);
            driver = pt.getProperty("driver");
            url = pt.getProperty("url");
            username = pt.getProperty("username");
            password = pt.getProperty("password");
        }catch (IOException e){
            e.printStackTrace();
        }
    }

 

第二種:ResourceBundle

private static String driver;
    private static String url;
    private static String username;
    private static String password;
    static{
        ResourceBundle bundle = ResourceBundle.getBundle("db");
        driver = bundle.getString("driver");
        url = bundle.getString("url");
        username = bundle.getString("username");
        password = bundle.getString("password");
    }

7.具體測試程式碼:

JDBCUtils.java

package pers.wpc.jdbc;

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

public class JDBCUtils {
    public static String driver;
    public static String url;
    public static String username;
    public static String password;
    static {
        Properties pt = new Properties();
        try{
            FileInputStream in = new FileInputStream("resources/db.properties");
            pt.load(in);
            driver = pt.getProperty("driver");
            url = pt.getProperty("url");
            username = pt.getProperty("username");
            password = pt.getProperty("password");
        }catch (IOException e){
            e.printStackTrace();
        }
    }
    /**
    *Properties
    public static String driver;
    public static String url;
    public static String username;
    public static String password;
    static {
        try{
            ClassLoader classLoader = JDBCUtils.class.getClassLoader();
            InputStream is = classLoader.getResourceAsStream("db.properties");
            Properties pt = new Properties();
            pt.load(is);
            driver = pt.getProperty("driver");
            url = pt.getProperty("url");
            username = pt.getProperty("username");
            password = pt.getProperty("password");
        }catch (IOException e){
            e.printStackTrace();
        }
    }*/
    /**
    *ResourceBundle
    private static String driver;
    private static String url;
    private static String username;
    private static String password;
    static{
        ResourceBundle bundle = ResourceBundle.getBundle("db");
        driver = bundle.getString("driver");
        url = bundle.getString("url");
        username = bundle.getString("username");
        password = bundle.getString("password");
    }*/
    public static Connection getConnection(){
        Connection conn = null;
        try {
            Class.forName(driver);
            conn= DriverManager.getConnection(url,username,password);
        }catch (Exception e){
            e.printStackTrace();
        }
        return conn;
    }
    public static void release(Connection conn, PreparedStatement pstmt, ResultSet rs){
        if (rs!=null){
            try {
                rs.close();
            }catch (SQLException e){
                e.printStackTrace();
            }
        }
        if (pstmt!=null){
            try {
                pstmt.close();
            }catch (SQLException e){
                e.printStackTrace();
            }
        }
        if (conn!=null){
            try {
                conn.close();
            }catch (SQLException e){
                e.printStackTrace();
            }
        }
    }
}

 TestUtils.java

package pers.wpc.jdbc;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class TestUtils {
    public static void main(String[] args) {
        Connection conn=null;
        PreparedStatement pstmt=null;
        ResultSet rs=null;
        try {
            conn =JDBCUtils.getConnection();
            String sql = "select * from t_book where bid=?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1,2);
            rs = pstmt.executeQuery();
            while (rs.next()){
                System.out.print(rs.getString(2)+"----"+rs.getString("author"));
            }
        }catch (SQLException e){
            e.printStackTrace();
        } finally {
            JDBCUtils.release(conn,pstmt,rs);
        }
    }
}