jdbc通过配置文件连接数据库

JDBC工具类通过配置文件获取数据库连接

配置文件的路径
	![在这里插入图片描述](https://img-blog.csdnimg.cn/20200211164329780.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ0NjQ4MTQ4,size_16,color_FFFFFF,t_70)
package JDBC.cn.wzk.jdbc.util;

import java.text.MessageFormat;
import java.util.Enumeration;
import java.util.List;
import java.util.MissingResourceException;
import java.util.ResourceBundle;

public class ConfigUtil {

    private ResourceBundle resourceBundle;

    public ConfigUtil(String bundleName) {
        resourceBundle = ResourceBundle.getBundle(bundleName);
    }

    public String getString(String key) {
        try {
            return resourceBundle.getString(key);
        } catch (MissingResourceException e) {
            e.printStackTrace();
            return "=> key is not exits:" + key;
        }
    }

    public String getString(String key, String[] paras) {
        try {
            String message = resourceBundle.getString(key);
            return MessageFormat.format(message, paras);
        } catch (MissingResourceException e) {
            return "=> key is not exits:" + key;
        }
    }

    public Enumeration<String> getKeys() {
        try {
            return resourceBundle.getKeys();
        } catch (MissingResourceException e) {
            e.printStackTrace();
            return null;
        }
    }

    public String getString(String key, List list) {
        try {
            if (!list.isEmpty()) {
                String[] paras = new String[list.size()];
                for (int i = 0; i < list.size(); i++)
                    paras[i] = (String) list.get(i);
                return getString(key, paras);
            }
            return "";
        } catch (MissingResourceException e) {
            return "=> key is not exits:" + key;
        }
    }

    public static void main(String[] args) {
        ConfigUtil config = new ConfigUtil("jdbc");

        Enumeration<String> keys = config.getKeys();
        while(keys.hasMoreElements()){
            String key = keys.nextElement();
            System.out.println("key:" + key + ", value:" + config.getString(key));
        }
    }
}

JDBC工具类

import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.sql.*;
import java.util.Enumeration;
import java.util.Properties;

public class JDBCUtil {

    private static String url;
    private static String user;
    private static String password;
    private static String driver;

    static {
        //读取资源文件,获取值
        try {

            ConfigUtil config = new ConfigUtil("jdbc");

            Enumeration<String> keys = config.getKeys();
            url = config.getString("url");
            user = config.getString("user");
            password = config.getString("password");
            driver = config.getString("driver");
            Class.forName(driver);
        }catch(ClassNotFoundException e){
            e.printStackTrace();
        }
    }

    public static Connection getConnection() throws SQLException {
        return DriverManager.getConnection(url,user,password);
    }

    public static void close(Statement statement,Connection conn){
        if(statement != null){
            try{
                statement.close();
            }catch (SQLException e){
                e.printStackTrace();
            }
        }
        if(conn != null) {
            try {
                conn.close();
            }catch (SQLException e){
                e.printStackTrace();
            }
        }
    }

    public static void close(ResultSet res, Statement statement, Connection conn){
        if(res != null){
            try{
                res.close();
            }catch (SQLException e){
                e.printStackTrace();
            }
        }
        if(statement != null){
            try{
                statement.close();
            }catch (SQLException e){
                e.printStackTrace();
            }
        }
        if(conn != null) {
            try {
                conn.close();
            }catch (SQLException e){
                e.printStackTrace();
            }
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值