package com.utils;
import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;
public class JdbcUtils {
private static String driver = null;
private static String url = null;
private static String username = null;
private static String password = null;
/**
* 工具类中的的构造方法都是私有的,
* 因为工具类中的方法都是静态的,不需要new,直接采用类名调用
*/
private JdbcUtils(){}
static { //静态代码块,类一加载就存在,并且只执行一次
try {
/*加载db.properties文件信息*/
InputStream inputStream = JdbcUtils.class.getClassLoader().getResourceAsStream("db.properties");
Properties properties = new Properties();
properties.load(inputStream);
/*获取对应的值*/
driver = properties.getProperty("driver");
url = properties.getProperty("url");
username = properties.getProperty("username");
password = properties.getProperty("password");
/*加载驱动*/
Class.forName(driver);
} catch (Exception e) {
e.printStackTrace();
}
}
//获取连接
public static Connection getConnection() throws Exception{
return DriverManager.getConnection(url,username,password);
}
//释放链接
public static void release(Connection connection, Statement statement, ResultSet resultSet) throws Exception{
if (connection!=null){
connection.close();
}
if (statement!=null){
statement.close();
}
if (resultSet!=null){
resultSet.close();
}
}
}
jdbc----提取公共类
最新推荐文章于 2024-02-19 13:12:29 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)