Druid database connection pooling tools

package cn.itcast.utils;

import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

/**
* @author newcityman
* @date 2019/8/16 - 23:56
* Druid 的工具类
*/
public class JDBCUtils {
// 1、定义成员变量DataSource
private static DataSource ds;

static {
{the try
// 2, load the configuration files
the Properties Pro = new new the Properties () ;
pro.load (. JDBCUtils . class.getClassLoader () the getResourceAsStream ( "druid.properties")) ;
//. 3, obtaining a connection object the Datasource DS = DruidDataSourceFactory . CreateDataSource (Pro) ; } the catch (IOException E) { e.printStackTrace () ; } the catch (Exception E) { e.printStackTrace () ; } } / * * Get method of connecting * * / public static connection getConnetion () SQLException {throws return ds.getConnection () ; } / *
















* 释放资源
* */
public static void close(Statement stmt,Connection conn){
/* if(stmt!=null){
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}

if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}*/
close(null,stmt,conn);
}

/*
* 释放资源
* */
public static void close(ResultSet rs,Statement stmt, Connection conn){
if(rs!=null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}

if(stmt!=null){
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}

if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}

}

/*
* 获取连接池方法
* */
public static DataSource getDataSource(){
return ds;
}

}

Guess you like

Origin www.cnblogs.com/newcityboy/p/11366983.html