Authored by qinchao

sql查询增加分页功能

@@ -131,10 +131,6 @@ public class SqlOperateServiceImpl implements SqlOperateService { @@ -131,10 +131,6 @@ public class SqlOperateServiceImpl implements SqlOperateService {
131 if(sql.endsWith(";")){ 131 if(sql.endsWith(";")){
132 sql = sql.substring(0,sql.length()-1); 132 sql = sql.substring(0,sql.length()-1);
133 } 133 }
134 - /*boolean descOrShowFlag=false;  
135 - if(sql.toLowerCase().startsWith("desc ")||sql.toLowerCase().startsWith("show ")){  
136 - descOrShowFlag=true;  
137 - }*/  
138 134
139 log.info("query4Table begin "+sql); 135 log.info("query4Table begin "+sql);
140 136
@@ -147,9 +143,12 @@ public class SqlOperateServiceImpl implements SqlOperateService { @@ -147,9 +143,12 @@ public class SqlOperateServiceImpl implements SqlOperateService {
147 } 143 }
148 int count =0; 144 int count =0;
149 Connection con = null; 145 Connection con = null;
150 - //PreparedStatement ps = null; 146 + boolean selectFlag=false;
151 Statement st = null; 147 Statement st = null;
152 ResultSet rs = null; 148 ResultSet rs = null;
  149 +
  150 + Statement st_pi = null;
  151 + ResultSet rs_pi = null;
153 try { 152 try {
154 String dbUrl="jdbc:mysql://" + sqlOperateModel.getDataSourceInfo() ; 153 String dbUrl="jdbc:mysql://" + sqlOperateModel.getDataSourceInfo() ;
155 if(StringUtils.isNotBlank(sqlOperateModel.getDbInfo())){ 154 if(StringUtils.isNotBlank(sqlOperateModel.getDbInfo())){
@@ -160,14 +159,30 @@ public class SqlOperateServiceImpl implements SqlOperateService { @@ -160,14 +159,30 @@ public class SqlOperateServiceImpl implements SqlOperateService {
160 159
161 con = DriverManager.getConnection(dbUrl, sqlOperateModel.getDataSourceUser(), sqlOperateModel.getDataSourcePwd()); 160 con = DriverManager.getConnection(dbUrl, sqlOperateModel.getDataSourceUser(), sqlOperateModel.getDataSourcePwd());
162 161
  162 + if(sql.toLowerCase().startsWith("select ")){
  163 + selectFlag=true;
  164 + rtnList.put("showPi",true);
  165 + rtnList.put("pageSizePi",MAX_QUERY_RESULT_COUNT);
  166 + rtnList.put("currentPagePi",sqlOperateModel.getCurrentPage());
  167 + st_pi = con.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
  168 + st_pi.setFetchSize(1);
  169 + st_pi.setMaxRows(1);
  170 + rs_pi = st_pi.executeQuery(" select count(1) from ( "+sql+" ) _tmp_sqloeaer_49_cnt ");
  171 + while (rs_pi.next()) {
  172 + rtnList.put("totalPi",rs_pi.getInt(1));
  173 + }
  174 + }else{
  175 + rtnList.put("showPi",false);
  176 + }
  177 +
163 st = con.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); 178 st = con.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
164 st.setFetchSize(FETCH_COUNT_FROM_MYSQL); 179 st.setFetchSize(FETCH_COUNT_FROM_MYSQL);
165 //不是desc的查询需要限定数量了 180 //不是desc的查询需要限定数量了
166 //前台有了分页,不再需要设定desc的限定数量 181 //前台有了分页,不再需要设定desc的限定数量
167 - //if(!descOrShowFlag){ 182 + if(selectFlag){
168 st.setMaxRows(MAX_QUERY_RESULT_COUNT*sqlOperateModel.getCurrentPage() +FETCH_COUNT_FROM_MYSQL); 183 st.setMaxRows(MAX_QUERY_RESULT_COUNT*sqlOperateModel.getCurrentPage() +FETCH_COUNT_FROM_MYSQL);
169 - // }  
170 - //st.setMaxRows(FETCH_COUNT_FROM_MYSQL); 184 + }
  185 +
171 rs = st.executeQuery(sql); 186 rs = st.executeQuery(sql);
172 rs.beforeFirst(); 187 rs.beforeFirst();
173 rs.relative(MAX_QUERY_RESULT_COUNT*sqlOperateModel.getCurrentPage()); 188 rs.relative(MAX_QUERY_RESULT_COUNT*sqlOperateModel.getCurrentPage());
@@ -221,8 +236,8 @@ public class SqlOperateServiceImpl implements SqlOperateService { @@ -221,8 +236,8 @@ public class SqlOperateServiceImpl implements SqlOperateService {
221 } 236 }
222 while (rs.next()) { 237 while (rs.next()) {
223 times++; 238 times++;
224 - //if(!descOrShowFlag&&times>MAX_QUERY_RESULT_COUNT){  
225 - if(times>MAX_QUERY_RESULT_COUNT){ 239 + if(selectFlag&&times>MAX_QUERY_RESULT_COUNT){
  240 + //if(times>MAX_QUERY_RESULT_COUNT){
226 queryDescr="数据量超过限制,本页只显示"+MAX_QUERY_RESULT_COUNT+"条"; 241 queryDescr="数据量超过限制,本页只显示"+MAX_QUERY_RESULT_COUNT+"条";
227 hasNext="yes"; 242 hasNext="yes";
228 break; 243 break;
@@ -282,6 +297,20 @@ public class SqlOperateServiceImpl implements SqlOperateService { @@ -282,6 +297,20 @@ public class SqlOperateServiceImpl implements SqlOperateService {
282 log.error("sqlOperateServiceImpl query4Table finally 关闭resultset异常:[" +sqlOperateModel.getQueryContent(),"sql="+sql+"]",e); 297 log.error("sqlOperateServiceImpl query4Table finally 关闭resultset异常:[" +sqlOperateModel.getQueryContent(),"sql="+sql+"]",e);
283 } 298 }
284 try { 299 try {
  300 + if(rs_pi!=null){
  301 + rs_pi.close();
  302 + }
  303 + } catch (SQLException e) {
  304 + log.error("sqlOperateServiceImpl query4Table finally 关闭resultset_pi异常:[" +sqlOperateModel.getQueryContent(),"sql="+sql+"]",e);
  305 + }
  306 + try {
  307 + if(st_pi!=null){
  308 + st_pi.close();
  309 + }
  310 + } catch (SQLException e) {
  311 + log.error("sqlOperateServiceImpl query4Table finally 关闭Statement_pi异常:[" + sqlOperateModel.getQueryContent(), "sql=" + sql + "]", e);
  312 + }
  313 + try {
285 if(st!=null){ 314 if(st!=null){
286 st.close(); 315 st.close();
287 } 316 }