使用数据库连接池技术的一个工具类
/*
使用数据库连接池
*/
public class JDBCUtils1 {
public static void main(String[] args) throws SQLException, IOException, ClassNotFoundException {
System.out.println(getConnection());
}
static DataSource dataSource;
static {
Properties properties=new Properties();
try {
properties.load(JDBCUtils1.class.getClassLoader().getResourceAsStream("druid.properties"));
dataSource = DruidDataSourceFactory.createDataSource(properties);
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
//获取连接
public static Connection getConnection() throws IOException, ClassNotFoundException, SQLException {
Connection connection = dataSource.getConnection();
return connection;
}
//关闭连接
public static void closeResources(Connection connection, Statement statement, ResultSet resultSet){
if (connection!=null){
try {
connection.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (statement!=null){
try {
statement.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (resultSet!=null){
try {
resultSet.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
}