Authored by qinchao

sql查询增加分页功能

... ... @@ -131,10 +131,6 @@ public class SqlOperateServiceImpl implements SqlOperateService {
if(sql.endsWith(";")){
sql = sql.substring(0,sql.length()-1);
}
/*boolean descOrShowFlag=false;
if(sql.toLowerCase().startsWith("desc ")||sql.toLowerCase().startsWith("show ")){
descOrShowFlag=true;
}*/
log.info("query4Table begin "+sql);
... ... @@ -147,9 +143,12 @@ public class SqlOperateServiceImpl implements SqlOperateService {
}
int count =0;
Connection con = null;
//PreparedStatement ps = null;
boolean selectFlag=false;
Statement st = null;
ResultSet rs = null;
Statement st_pi = null;
ResultSet rs_pi = null;
try {
String dbUrl="jdbc:mysql://" + sqlOperateModel.getDataSourceInfo() ;
if(StringUtils.isNotBlank(sqlOperateModel.getDbInfo())){
... ... @@ -160,14 +159,30 @@ public class SqlOperateServiceImpl implements SqlOperateService {
con = DriverManager.getConnection(dbUrl, sqlOperateModel.getDataSourceUser(), sqlOperateModel.getDataSourcePwd());
if(sql.toLowerCase().startsWith("select ")){
selectFlag=true;
rtnList.put("showPi",true);
rtnList.put("pageSizePi",MAX_QUERY_RESULT_COUNT);
rtnList.put("currentPagePi",sqlOperateModel.getCurrentPage());
st_pi = con.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
st_pi.setFetchSize(1);
st_pi.setMaxRows(1);
rs_pi = st_pi.executeQuery(" select count(1) from ( "+sql+" ) _tmp_sqloeaer_49_cnt ");
while (rs_pi.next()) {
rtnList.put("totalPi",rs_pi.getInt(1));
}
}else{
rtnList.put("showPi",false);
}
st = con.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
st.setFetchSize(FETCH_COUNT_FROM_MYSQL);
//不是desc的查询需要限定数量了
//前台有了分页,不再需要设定desc的限定数量
//if(!descOrShowFlag){
if(selectFlag){
st.setMaxRows(MAX_QUERY_RESULT_COUNT*sqlOperateModel.getCurrentPage() +FETCH_COUNT_FROM_MYSQL);
// }
//st.setMaxRows(FETCH_COUNT_FROM_MYSQL);
}
rs = st.executeQuery(sql);
rs.beforeFirst();
rs.relative(MAX_QUERY_RESULT_COUNT*sqlOperateModel.getCurrentPage());
... ... @@ -221,8 +236,8 @@ public class SqlOperateServiceImpl implements SqlOperateService {
}
while (rs.next()) {
times++;
//if(!descOrShowFlag&&times>MAX_QUERY_RESULT_COUNT){
if(times>MAX_QUERY_RESULT_COUNT){
if(selectFlag&&times>MAX_QUERY_RESULT_COUNT){
//if(times>MAX_QUERY_RESULT_COUNT){
queryDescr="数据量超过限制,本页只显示"+MAX_QUERY_RESULT_COUNT+"条";
hasNext="yes";
break;
... ... @@ -282,6 +297,20 @@ public class SqlOperateServiceImpl implements SqlOperateService {
log.error("sqlOperateServiceImpl query4Table finally 关闭resultset异常:[" +sqlOperateModel.getQueryContent(),"sql="+sql+"]",e);
}
try {
if(rs_pi!=null){
rs_pi.close();
}
} catch (SQLException e) {
log.error("sqlOperateServiceImpl query4Table finally 关闭resultset_pi异常:[" +sqlOperateModel.getQueryContent(),"sql="+sql+"]",e);
}
try {
if(st_pi!=null){
st_pi.close();
}
} catch (SQLException e) {
log.error("sqlOperateServiceImpl query4Table finally 关闭Statement_pi异常:[" + sqlOperateModel.getQueryContent(), "sql=" + sql + "]", e);
}
try {
if(st!=null){
st.close();
}
... ...