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);
        }
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_42039699/article/details/84786756
今日推荐