JDBC-通过配置文件properties-获取数据库链接Connection

2022/4/13 2:12:31

本文主要是介绍JDBC-通过配置文件properties-获取数据库链接Connection,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

一、创建一个util模块:如图

二、JDBCUtil工具类

jdbc_url=jdbc:mysql://localhost:3306
jdbc_driver=com.mysql.jdbc.Driver
jdbc_user=root
jdbc_password=root

三、创建Env组件获取配置文件信息

/**
*属性配置文件的读取
*
*/
package com.xzit.platfrom.util;

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

public class Env extends Properties {
    //获取jdbc驱动
    public static String driver;
    //获取jdbc解析地址
    public static String url;
    //获取用户名
    public static String user;
    //获取密码
    public static String password;

    static {
        
        InputStream resourceAsStream = Env.class.
                getResourceAsStream("dbconf.properties");
        try {

            //获取配置文件
            Properties properties = new Properties();
            properties.load(resourceAsStream);
            url = properties.getProperty("jdbc_url");
            driver = properties.getProperty("jdbc_driver");
            user = properties.getProperty("jdbc_user");
            password = properties.getProperty("jdbc_password");
            System.out.println("数据库驱动:"+driver);
            System.out.println("数据库url:"+url);
            System.out.println("数据库用户名:"+user);
            System.out.println("数据库密码:"+password);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

四、写一个数据库管理类

package com.xzit.platfrom.util;

import java.sql.*;

/**
 * JDBC工具类简化JDBC编程
 * */
public class DataSourceManager {
    /*私有化工具类方法*/
    private DataSourceManager (){}

    /**
     * 获取数据库连接对象
     * @return 数据库链接对象
     * @throws SQLException
     * */
    public static Connection getConnection () throws Exception {
        return DriverManager.getConnection(Env.url,Env.user,Env.password);

        //Connection conn = null;
        //try {
        //   Class.forName(Env.driver);
        //  conn = DriverManager.getConnection(Env.url,Env.user,Env.password);
        //} catch (Exception e) {
        //  e.printStackTrace();
        //}
        //return conn;
    }

    /**
     * 关闭资源
     * @param conn
     */
    public static  void close(Connection conn){
        try {
            if(conn !=null){
                conn.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 关闭资源
     * @param state
     */
    public static void close( Statement state ){
        try {
            if(state !=null ){
                state.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 关闭结果集
     * @param set
     */
    public static void close( ResultSet set ){
        try {
            if(set  !=null ){
                set .close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

五、测试运行数据库连接结果

package com.xzit.platfrom.util;


import static com.xzit.platfrom.util.DataSourceManager.getConnection;

public class Test {
    public static void main ( String[] args ) throws Exception {
        System.out.println(getConnection());
    }
}

 



这篇关于JDBC-通过配置文件properties-获取数据库链接Connection的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程