关闭

java连接oracle数据库 -- 直接用jsp访问连接

标签: jsprownum分页jsp分页处理jsp的oracle连接
1541人阅读 评论(0) 收藏 举报
分类:

1. 创建jsp文件

2. import加入sql包

3. 编写主要的jsp文件

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

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</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">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
  </head>
  
  <body>
    <body>
  <h2>oracle的分页案例</h2>
  <table>
  <tr>
  <td>用户名</td>
  <td>薪水</td>
  </tr>
  <%
  			Class.forName("oracle.jdbc.driver.OracleDriver");
			Connection cc = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:MyOra1","scott","tiger");
			Statement  sm = cc.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 countSize=4;
			
			ResultSet rs = sm.executeQuery("select count(*) from emp");
			if(rs.next()){
				rowCount = rs.getInt(1);
				if(rowCount%countSize==0){
					pageCount = rowCount/countSize;
				}else{
					pageCount = rowCount/countSize + 1 ;
				}
			}
			rs = sm.executeQuery("select * from (select a1.*,rownum rn from (select * from emp) a1  where rownum<="+pageNow*countSize+") where rn>="+((pageNow-1)*countSize+1)+"");
			
			while(rs.next()){
				out.print("<tr>");
				out.println("<td>" + rs.getString(2)+"</td>");
				out.println("<td>" + rs.getString(6)+"</td>");
				out.print("</tr>");
				
			}
			for(int i=1;i<=pageCount;i++){
					out.print("<a href=index.jsp?pageNow="+i+">["+i+"]</a>");
				}
			//打印总页数
		
			rs.close();
			sm.close();
			cc.close();
   %>
  </table>
  </body>
  </body>
</html>

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:95263次
    • 积分:512
    • 等级:
    • 排名:千里之外
    • 原创:19篇
    • 转载:1篇
    • 译文:2篇
    • 评论:10条
    文章存档
    最新评论