package com.vcredit.ddcash.batch.util;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.apache.log4j.Logger;
import com.vcredit.ddcash.batch.model.entity.ModuleConfigEntity;
/**
* 读取数据库管理的配置
*
* @author xutao
*/
public class DBConfigReader {
private static final Logger LOGGER = Logger.getLogger(DBConfigReader.class);
/**
* 数据库连接
*/
private static Connection connection = null;
private static final String CONFIG_QUERY_SQL = "SELECT ID id,CONFIG_NAME configName,CONFIG_DESC configDesc,CONFIG_VALUE configValue,PID pid,REMARK remark,DELETE_FLAG deleteFlag,CREATED_DATE createdDate,UPDATED_DATE updatedDate FROM t_ddq_module_config WHERE CONFIG_NAME = ? AND DELETE_FLAG = ‘0‘";
static {
initConnection();
}
/**
* 初始化数据库连接
*/
private static void initConnection() {
if (connection == null) {
connection = DBHelper.getConnection();
LOGGER.info("初始化数据库连接完成");
}
}
/**
* 根据配置名查找配置值
*
* @param param_configName
* @return
*/
public static String getValueByName(String param_configName) {
LOGGER.info("RequestParam:" + param_configName);
initConnection();
PreparedStatement ps = null;
ResultSet rs = null;
try {
ps = connection.prepareStatement(CONFIG_QUERY_SQL);
ps.setString(1, param_configName);
rs = ps.executeQuery();
rs.next();
ModuleConfigEntity result = getResultEntity(rs);
String value = result.getConfigValue();
LOGGER.info("配置名: 【" + param_configName + "】 ===> 配置值 【" + value + "】 ");
return value;
} catch (SQLException e) {
LOGGER.error("请求配置【" + param_configName + "】时发生异常!", e);
} finally {
DBHelper.closeAll(null, ps, rs);
}
return null;
}
/**
* 关闭数据库连接
*/
public static void closeConnection() {
try {
if (null != connection) {
connection.close();
}
LOGGER.info("DBConfigReader的connection被关闭!");
} catch (SQLException e) {
LOGGER.error("关闭DBConfigReader的connection时发生异常!", e);
}
}
/**
* 设置结果对象
*
* @param rs
* @return
* @throws SQLException
*/
private static ModuleConfigEntity getResultEntity(ResultSet rs) throws SQLException {
ModuleConfigEntity resultEntity = new ModuleConfigEntity();
resultEntity.setId(rs.getLong("id"));
resultEntity.setConfigName(rs.getString("configName"));
resultEntity.setConfigDesc(rs.getString("configDesc"));
resultEntity.setConfigValue(rs.getString("configValue"));
resultEntity.setpId(rs.getLong("pid"));
resultEntity.setRemark(rs.getString("remark"));
resultEntity.setDeleteFlag(rs.getString("deleteFlag"));
resultEntity.setCreatedDate(rs.getString("createdDate"));
resultEntity.setUpdatedDate(rs.getString("updatedDate"));
return resultEntity;
}
}