列表页使用sql语句后怎样分页
发布网友
发布时间:2022-05-04 10:39
我来回答
共2 个回答
懂视网
时间:2022-05-04 15:00
(1). top ... not in , (2). top .... id( max ),(3).游标 这种方法感觉比上面三种要快 ,分享一下跟好的意见 使用的是org.springframework.jdbc.core. JdbcTemplate limit = 25; public ListMapString, Object findAll(String type, final int start, f
(1). top ... not in , (2). top .... id>(max),(3).游标
这种方法感觉比上面三种要快 ,分享一下跟好的意见
使用的是 org.springframework.jdbc.core.JdbcTemplate
limit = 25;
public List> findAll(String type, final int start, final int limit) {
StringBuilder builder = new StringBuilder();
List params = new ArrayList();//给占位符赋值
builder.append("select top " + (start + limit)//第一页,start=0 ()select top 25 * ,第二页 start=25 (select top 50 *)
+ " * from v_fulldata a where 1=1");
if (type != null && type != " ") {
builder.append(" and a.atype = ? ");
params.add(type);
}
builder.append(" order by wg13 desc;");
String sql = builder.toString();
List> list = (List>) getJdbc()
.query(sql, params.toArray(), new ResultSetExtractor() {
@Override
public Object extractData(ResultSet rs)
throws SQLException, DataAccessException {
List> list = new ArrayList>();
while (rs.next()) { //将查询结果循环
if (rs.getRow() < start) { //排除小于开始索引的数据
continue;
}
Map row = new HashMap();
ResultSetMetaData rsmd = rs.getMetaData();
for (int i = 1; i <= rsmd.getColumnCount(); i++) {
row.put(rsmd.getColumnName(i), rs.getString(i));
}
list.add(row);
}
return list;
}
});
return list;
}
热心网友
时间:2022-05-04 12:08
public class UserDao { String url = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=db_database04"; //url,数据库 String username="sa"; //用户名 String password=""; //密码 private Connection con = null; private Statement stmt = null; private ResultSet rs = null; public UserDao() { //通过构造方法加载数据库驱动 try { Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver"); } catch (Exception ex) { System.out.println("数据库加载失败"); } } public boolean Connection() { //创建数据库连接 try { con = DriverManager.getConnection(url, username, password); } catch (SQLException e) { System.out.println(e.getMessage()); System.out.println("creatConnectionError!"); } return true; } public ResultSet selectStatic(String sql) throws SQLException { //对数据库的查询操作 ResultSet rs=null; if (con == null) { Connection(); } try { stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE); rs = stmt.executeQuery(sql); } catch (SQLException e) { e.printStackTrace(); } return rs; } public void closeConnection() { //关闭数据库的操作 if (con != null && stmt != null && rs != null) { try { rs.close(); stmt.close(); con.close(); } catch (SQLException e) { e.printStackTrace(); System.out.println("Failed to close connection!"); } finally { con = null; } } } }