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

Oracle查询分页

阅读更多

今天做了一个JDBC连接Oracle,其实和连接别的数据库很相似,关键是分页。初步实现了功能,条理不够清楚,有什么疑问可以给我留言,大家共同进步。

<%@ page language="java" import="java.util.*,java.sql.*"
	pageEncoding="UTF-8"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>
<html>
	<head>
		<base href="<%=basePath%>">

		<title>Oracle 分页</title>

		<meta http-equiv="pragma" content="no-cache">
		<meta http-equiv="cache-control" content="no-cache">
		<meta http-equiv="expires" content="0">
		<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
		<meta http-equiv="description" content="This is my page">

	</head>

	<body>
		<%
			ResultSet rs = null;
			Statement stmt = null;
			Connection conn = null;
			try {
				String URL = "jdbc:oracle:thin:@localhost:1521:myoracl";
				String user = "scott";
				String password = "tiger";
				Class.forName("oracle.jdbc.driver.OracleDriver");
				conn = DriverManager.getConnection(URL, user, password);
				stmt = conn.createStatement();

				String s_pagenow = (String) request.getParameter("pagenow");
				int pagenow = 1;
				if (s_pagenow != null) {
					pagenow = Integer.parseInt(s_pagenow);
				}
				int pagecount = 0; //
				int rowcount = 0; //共有几条记录
				int pagesize = 3; //每页显示几条记录

				rs = stmt.executeQuery("select count(*) from emp");
				if (rs.next()) {
					rowcount = rs.getInt(1);
					if (rowcount % pagesize == 0) {
						pagecount = rowcount / pagesize;
					} else {
						pagecount = rowcount / pagesize + 1;
					}
					System.out.println(rowcount);
				}
				rs = stmt
						.executeQuery("select * from ( select a1.*,rownum rn from (select * from emp) a1 where rownum<="
								+ (pagenow * pagesize)
								+ ") where rn>="
								+ ((pagenow - 1) * pagesize + 1));
				out.print("<table><tr><td>职工号</td><td>姓名</td></tr>");
				while (rs.next()) {
					out.print("<tr>");
					out.print("<td>" + rs.getString("empno") + "</td>");
					out.print("<td>" + rs.getString("ename") + "</td>");
					out.print("</tr>");
				}
				out.print("</table>");
				for (int i = 1; i <= pagecount; i++) {
					out.print("<a href=OracleFenye.jsp?pagenow=" + i + ">[" + i
							+ "]</a>");
				}
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			} catch (SQLException e) {
				e.printStackTrace();
			} finally {
				try {
					if (rs != null) {
						rs.close();
						rs = null;
					}
					if (stmt != null) {
						stmt.close();
						stmt = null;
					}
					if (conn != null) {
						conn.close();
						conn = null;
					}
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		%>
	</body>
</html>

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics