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