`
carrot
  • 浏览: 160781 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

数据库分页查询

SQL 
阅读更多
public class SQLServerPages extends Pages { public SQLServerPages() { } public ResultSet getRs(TranContext DBSQL, String pk) { String SQL = ""; ResultSet rs = null; try { SQL = this.getSQLString(pk); DBSQL.prepareStatement(SQL); rs = DBSQL.executeQuery(); return rs; } catch (Exception e) { return null; } } public String getSQLString(String pk) { String sql = this.sSQL.toLowerCase(Sys.getLocale()); String sqltotal = ""; String con1 = "select"; String con2 = "from"; String con3 = "where"; String con4 = "order"; int i1 = sql.indexOf(con1); int i2 = sql.indexOf(con2); int i3 = sql.indexOf(con3); int i4 = sql.indexOf(con4); String selectitem = ""; String tablename = ""; String cond = ""; String orders = ""; selectitem = sql.substring(i1 + 6, i2); if (i3 != -1) { tablename = sql.substring(i2 + 4, i3); if (i4 != -1) { cond = sql.substring(i3 + 5, i4); orders = sql.substring(i4 + 5, sql.length()); } else { cond = sql.substring(i3 + 5, sql.length()); } } else { if (i4 != -1) { tablename = sql.substring(i2 + 4, i4); orders = sql.substring(i4 + 5, sql.length()); } else { tablename = sql.substring(i2 + 4, sql.length()); } } String _orders = ""; if (i4 != -1) { _orders = "order " + orders; } //sql = "select Top "+ipage+" * from "+tablename+" where ID NOT IN (select Top "+(spage+ipage)+" ID from "+tablename+" "+_orders+") "+_orders; sql = "select Top " + perpagenum + " * from " + tablename + " where "; if (i3 != -1) { sql += "(" + cond + ") and "; } sql += pk + " NOT IN (select Top " + spage + " " + pk + " from " + tablename + " "; if (i3 != -1) { sql += "where " + cond; } sql += _orders + ") " + _orders; return sql; } }
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics