tools.c3p0.C3P0Util Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of java-autotest-tool Show documentation
Show all versions of java-autotest-tool Show documentation
This is an integration of autotest tools
package tools.c3p0;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import tools.c3p0.database.DBConnectionManager;
import tools.c3p0.database.DatabaseAccessFactory;
import tools.c3p0.database.DatabaseAccessInterface;
import tools.c3p0.orm.OrmUtil;
import tools.config.SystemConfig;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @author: zhengyu
* @date: 2018/11/2
* @protocol: http 或 thrift
* @apiName: /uri 或 服务名.接口名
* @description:
*/
public class C3P0Util {
private static final Logger logger = LoggerFactory.getLogger(C3P0Util.class);
public static Map dbMap = new HashMap<>();
public static void init(String propertiesPath, String...dbNameGroup){
/*初始化全局配置,写自己的配置文件位置。*/
SystemConfig.setConfigLocation(propertiesPath);/*test/config*/
int length = dbNameGroup.length;
if (length > 0){
for(String dbInfo : dbNameGroup){
C3P0Util.dbMap.put(dbInfo, DatabaseAccessFactory.getDataAccessInstance(dbInfo));
}
logger.info(length+"个数据库连接池初始化成功。");
}else {
logger.error("您输入的dbNameGroup数组的长度<=0。");
}
}
/* executeQueryStmt 的各项重载方法*/
public static ResultSet executeQueryStmt(String dbInfo, String querySql){
try {
return C3P0Util.dbMap.get(dbInfo).executeQueryStmt(querySql);
} catch (SQLException e) {
e.printStackTrace();
return null;
}
}
public static String executeQueryStmt(String dbInfo, String querySql, String fieldName){
DatabaseAccessInterface dai = C3P0Util.dbMap.get(dbInfo);
String resutlStr = "";
try {
dai.executeQueryStmt(querySql);
resutlStr = dai.getSpecificVal(fieldName);
} catch (SQLException e) {
e.printStackTrace();
}
return resutlStr;
}
public static Map executeQueryStmt(String dbInfo, String querySql, String...fieldGroup){
DatabaseAccessInterface dai = C3P0Util.dbMap.get(dbInfo);
try {
dai.executeQueryStmt(querySql);
return dai.getSpecificVal(fieldGroup);
} catch (SQLException e) {
e.printStackTrace();
return null;
}
}
public static PreparedStatement createPreparedStmt(String dbInfo, String preparedSql){
try {
return C3P0Util.dbMap.get(dbInfo).createPreparedStmt(preparedSql);
} catch (SQLException e) {
e.printStackTrace();
return null;
}
}
/* executeQueryPreparedStmt 的各项重载方法*/
public static ResultSet executeQueryPreparedStmt(String dbInfo, PreparedStatement preparedStatement, Object...parameters){
try {
return C3P0Util.dbMap.get(dbInfo).executeQueryPreparedStmt(preparedStatement,parameters);
} catch (SQLException e) {
e.printStackTrace();
return null;
}
}
public static String executeQueryPreparedStmt(String dbInfo, PreparedStatement preparedStatement, String fieldName, Object...parameters){
DatabaseAccessInterface dai = C3P0Util.dbMap.get(dbInfo);
String resutlStr = "";
try {
dai.executeQueryPreparedStmt(preparedStatement,parameters);
resutlStr = dai.getSpecificVal(fieldName);
} catch (SQLException e) {
e.printStackTrace();
}
return resutlStr;
}
public static Map executeQueryPreparedStmt(String dbInfo, PreparedStatement preparedStatement, String[] fieldGroup, Object...parameters){
DatabaseAccessInterface dai = C3P0Util.dbMap.get(dbInfo);
try {
dai.executeQueryPreparedStmt(preparedStatement,parameters);
return dai.getSpecificVal(fieldGroup);
} catch (SQLException e) {
e.printStackTrace();
return null;
}
}
/* executeUpdateStmt 方法*/
public static int executeUpdateStmt(String dbInfo, String updateSql){
try {
return C3P0Util.dbMap.get(dbInfo).executeUpdateStmt(updateSql);
} catch (SQLException e) {
e.printStackTrace();
return 0;
}
}
/* executeUpdatePreparedStmt 方法*/
public static int executeUpdatePreparedStmt(String dbInfo, PreparedStatement preparedStatement, Object...parameters){
try {
return C3P0Util.dbMap.get(dbInfo).executeUpdatePreparedStmt(preparedStatement,parameters);
} catch (SQLException e) {
e.printStackTrace();
return 0;
}
}
/*打印ResultSet*/
public static void printResultSet(String dbInfo){
try {
C3P0Util.dbMap.get(dbInfo).printRS();
} catch (SQLException e) {
e.printStackTrace();
}
}
/* 释放某一个库的链接池 */
public static void releaseOnePool(String dbInfo){
DBConnectionManager.release(dbInfo);
}
/* 释放全部数据库的链接池 */
public static void releaseAllPools(){
DBConnectionManager.release();
}
/* The following is for ORM usage */
public static void setXmlConfig(String mapperXmlPath){
OrmUtil.setXmlMapperPath(mapperXmlPath);
}
public static List executeSelectByMap(String dbInfo, String id, Map paramMap, Class targetClass) throws ClassNotFoundException, SQLException, InstantiationException, IllegalAccessException {
OrmUtil.analyzeDbXml();
JSONObject sqlJSONObject = OrmUtil.getSqlInfo(id);
if (sqlJSONObject != null && !sqlJSONObject.isEmpty()){
String sql = sqlJSONObject.getString("text");
String parameterType = sqlJSONObject.getJSONObject("attribute").getString("parameterType");
Map stmtParamMap = OrmUtil.analyzeSql(sql, parameterType, paramMap);
ResultSet resultSet = null;
if (stmtParamMap.containsKey("PreparedStatement")){
PreparedStatement preparedStatement = C3P0Util.createPreparedStmt(dbInfo, stmtParamMap.get("PreparedStatement").toString());
List