info.unclewang.DbUtilsDruid Maven / Gradle / Ivy
The newest version!
package info.unclewang;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import com.alibaba.druid.pool.DruidPooledConnection;
import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;
import org.apache.commons.dbutils.handlers.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import java.util.Properties;
/**
* unclewang
* 2018/5/4 09:59
* QueryRunner类、DBUtils类和Druid数据库连接池综合使用
*/
public class DbUtilsDruid {
private static Logger logger = LoggerFactory.getLogger(DbUtilsDruid.class);
private static DruidDataSource dds = null;
private static DruidPooledConnection con = null;
private static String filepath = "db.properties";
private static class SingletonHolder {
private static final DbUtilsDruid INSTANCE = new DbUtilsDruid();
}
public static final DbUtilsDruid getInstance() {
return SingletonHolder.INSTANCE;
}
private DbUtilsDruid() {
init(filepath);
}
private void init(String filepath) {
Properties properties = loadPropertyFile(filepath);
try {
dds = (DruidDataSource) DruidDataSourceFactory
.createDataSource(properties);
con = dds.getConnection();
} catch (Exception e) {
e.printStackTrace();
}
}
public static Properties loadPropertyFile(String fullFile) {
Properties p = new Properties();
if (fullFile == "" || fullFile.equals("")) {
System.out.println("属性文件为空!");
} else {
InputStream inStream = DbUtilsDruid.class.getClassLoader().getResourceAsStream(fullFile);
try {
p.load(inStream);
} catch (IOException e) {
e.printStackTrace();
}
}
return p;
}
public DruidPooledConnection getCon() {
return con;
}
public static String getFilepath() {
return filepath;
}
public static void setFilepath(String filepath) {
DbUtilsDruid.filepath = filepath;
}
/**
* 把如果修改了db.properties文件路径,则应该 先DbUtilsDruid.setFilepath("db_new.properties");
* 连接则把dpc传进去
* DbUtilsDruid dd = DbUtilsDruid.getInstance();
* Connection connection = dd.getCon();
*/
public int crud(Connection dpc, String sql, Object... param) {
int row = 0;
QueryRunner qr = new QueryRunner();
try {
row = qr.update(dpc, sql, param);
logger.info(String.valueOf(row));
} catch (SQLException e) {
e.printStackTrace();
}
return row;
}
/**
* 如果db.properties文件放在默认的resources文件夹下,则用默认的crul方法
*/
public int crud(String sql, Object... param) {
DruidPooledConnection dpc = DbUtilsDruid.getInstance().getCon();
return this.crud(dpc, sql, param);
}
public int crud(Connection dpc, String sql, Object[][] param) {
int row = 0;
QueryRunner qr = new QueryRunner();
try {
row = qr.batch(dpc, sql, param).length;
} catch (SQLException e) {
e.printStackTrace();
} catch (ArrayIndexOutOfBoundsException e) {
e.printStackTrace();
}
return row;
}
public int crud(String sql, Object[][] param) {
DruidPooledConnection dpc = DbUtilsDruid.getInstance().getCon();
return this.crud(dpc, sql, param);
}
public Object query(String sql, ResultSetHandler rsh, Object... param) {
DruidPooledConnection dpc = DbUtilsDruid.getInstance().getCon();
return this.query(dpc, sql, rsh, param);
}
/**
* 把如果修改了db.properties文件路径,则应该 先DbUtilsDruid.setFilepath("db_new.properties");
* 连接则把dpc传进去
* DbUtilsDruid dd = DbUtilsDruid.getInstance();
* Connection connection = dd.getCon();
*/
public Object query(Connection dpc, String sql, ResultSetHandler rsh, Object... param) {
Object result = "";
QueryRunner qr = new QueryRunner();
try {
if (rsh instanceof MapListHandler) {
List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy