String url="jdbc:mysql://127.0.0.1:3306/dongyu";
conn= DriverManager.getConnection(url,"root","");
stmt = conn.createStatement();
rs = stmt.executeQuery("select * from t_statistics order by id");
rs.last();
int totalcount = rs.getRow();
int currentPage = 2;
int resultNum = 5;
int startIndex = (currentPage-1)*resultNum;
rs = stmt.executeQuery("select * from t_statistics limit "+startIndex+","+resultNum+"");
//rs = stmt.executeQuery("select * from t_viscera_picture where id=(select max(id) from t_viscera_picture) for update");
System.out.println("共"+totalcount+"条");
while(rs.next()) {
System.out.println(rs.getString("id"));
}
conn= DriverManager.getConnection(url,"root","");
stmt = conn.createStatement();
rs = stmt.executeQuery("select * from t_statistics order by id");
rs.last();
int totalcount = rs.getRow();
int currentPage = 2;
int resultNum = 5;
int startIndex = (currentPage-1)*resultNum;
rs = stmt.executeQuery("select * from t_statistics limit "+startIndex+","+resultNum+"");
//rs = stmt.executeQuery("select * from t_viscera_picture where id=(select max(id) from t_viscera_picture) for update");
System.out.println("共"+totalcount+"条");
while(rs.next()) {
System.out.println(rs.getString("id"));
}
本文展示了一个使用 Java 进行 MySQL 数据库操作的例子,主要实现了数据分页查询的功能。通过设置每页显示的记录数及当前页码,从 t_statistics 表中获取指定范围的数据,并打印总记录数及每条记录的 ID。

2166

被折叠的 条评论
为什么被折叠?



