连接方法
public class ConUtil {
private static Map<String, DaoUp> daoUpMap = new HashMap<String, DaoUp>();
public static DaoUp getNewConnect(String datasource) {
DaoUp daoUp = null;
if(daoUpMap.get(datasource) != null){
return daoUpMap.get(datasource);
}else{
try {
UrlDao urlDao = Mvcs.ctx().getDefaultIoc().get(UrlDao.class, "urlDao");
if(eastUrlDao != null){
Url url = urlDao.fetch(datasource);
if(url != null){
DruidDataSource druidDataSource = new DruidDataSource();
druidDataSource.setDriverClassName(url.getDriver());
druidDataSource.setUrl(url.getUrlStr());
druidDataSource.setUsername(url.getUserName());
druidDataSource.setPassword(uUrl.getPassword());
druidDataSource.setMaxActive(250);
druidDataSource.setMinIdle(5);
druidDataSource.getConnection();
daoUp = new DaoUp(datasource){
};
daoUp.setDataSource(druidDataSource);
daoUpMap.put(datasource, daoUp);
return daoUp;
}else{
return null;
}
}else{
return null;
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
return daoUp;
}
}
调用连接
public Object getAllBm(@Param("token")String token,HttpServletRequest req){
try{
DaoUp daoUp = ConUtil.getNewConnect("YY"); //连接过后我还需要调用daoUp.close()吗?
String sqlStr = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
Sql sql = Sqls.create(sqlStr);
sql.setCallback(new SqlCallback() {
@Override
public Object invoke(Connection conn, ResultSet rs, Sql sql)
throws SQLException {
List<Map<String, String>> branchList = new ArrayList<Map<String,String>>();
while (rs.next()) {
Map<String, String> branchMap = new HashMap<String, String>();
branchMap.put("branchId", rs.getString("brnnbr"));
branchMap.put("branchName", rs.getString("brnname"));
branchMap.put("pId", rs.getString("upperbrn"));
branchList.add(branchMap);
}
return branchList;
}
});
daoUp.dao().execute(sql);
List<Map> branchList = sql.getList(Map.class);
return requestResult(true, branchList);
}catch(Exception e){
return requestResult(false, e.getMessage());
}
}
/连接过后我还需要调用daoUp.close()吗? daoUp.close() 关闭的又是什么?