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

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>

你可能感兴趣的:(jsp,rownum分页,jsp的oracle连接,jsp分页处理)