1.鎴戠殑鏁版嵁搴撳悕鍙玜aa錛屽瘑鐮佹槸123
2.紜畾鏁版嵁搴撲腑鏈夋暟鎹?榪欐槸鏌ヨ鍔熻兘+鍒嗛〉
鍒涘緩鏁版嵁搴搒ql
CREATE TABLE users (
username varchar2(100),
department varchar2(100),
headship varchar2(100),
)
綾繪斁鍦╯rc涓嬬殑com鍖呬腑
db.java
package com;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class db {
public static Connection getConnection() throws ClassNotFoundException, SQLException
{
Connection con = null;
Class.forName("oracle.jdbc.driver.OracleDriver");
String url = "jdbc:oracle:thin:@浣犵殑ip:1521:aaa";
con = DriverManager.getConnection(url, "SYSTEM","123");
return con;
}
}
鏌ヨ欏甸潰MyJsp.jsp
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<%@ page import="java.sql.*"%>
<jsp:directive.page import="com.*;"/>
<%
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 'a.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>
<%!
int pageSize = 5;//姣忛〉鏄劇ず鐨勮褰曟暟
int pageCount = 0;//鎬婚〉鏁?br />
%>
<%!
Connection con;
Statement sql;
ResultSet rs;
%>
<%
try
{
con = db.getConnection();
Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);//鍙粴鍔ㄦ煡璇㈡暟鎹殑緇撴灉闆?br />
request.setCharacterEncoding("GB2312");
rs = stmt.executeQuery("select * from users") ;
rs.last(); //璁╂父鏍囧埌琛ㄤ腑鐨勬渶鍚庝竴琛?br />
int rowCount = rs.getRow(); //鑾峰彇璁板綍鎬繪暟.
pageCount = (rowCount % pageSize == 0) ? (rowCount / pageSize ) : (rowCount / pageSize +1);
int showPage = 1;//褰撳墠欏?br />
//鍙栧緱鐢ㄦ埛鎵鎸囧畾鐨勯〉
String goToPage = request.getParameter("showPage");
if (goToPage == null){
goToPage = "1";
}
//杞崲鎴愭暣褰?br />
try{
showPage = Integer.parseInt(goToPage);
}
catch (NumberFormatException ex){
showPage = 1;
}
//褰撳墠欏靛皬浜庣瓑浜庣涓欏靛垯鎸夌涓欏電畻 濡傛灉 褰撳墠欏靛ぇ浜庣瓑浜庢婚〉鏁板垯涓烘渶鍚庨〉
if(showPage <=1){
showPage = 1;
}
else if(showPage >= pageCount){
showPage = pageCount;
}
//娓告爣鐨勪綅緗?(褰撳墠欏?- 1) * 欏甸潰澶у皬 + 1
int posion = (showPage -1 ) * pageSize + 1;
//璁劇疆娓告爣鐨勪綅緗?br />
rs.absolute(posion);
%>
<table border="1">
<tr>
<td width="160">鐢ㄦ埛鍚?lt;/td>
<td width="160">閮ㄩ棬</td>
<td width="160">鑱屼綅</td>
</tr>
</table>
<%
int i =0;
//寰幆鏄劇ず琛ㄤ腑鐨勬暟鎹?pageSize(姣忛〉鎵鏄劇ず鐨勮褰?
//rs.isAfterLast() 娓告爣鏄惁鍦ㄦ渶鍚庝竴琛屼箣鍚庤鏄庡悗闈㈠凡緇忔病璁板綍
while(i<pageSize && !rs.isAfterLast()){
%>
<table border="1">
<col width="160px"/><col width="160px"/><col width="160px"/><col width="160px"/>
<tr>
<td><%=rs.getString("username")%></td>
<td><%=rs.getString("department")%></td>
<td><%=rs.getString("headship")%></td>
</tr>
<%rs.next();i++;}%>
</table>
<form action="" method="get">
<table border="1">
<tr>
<td>褰撳墠絎?lt;%=showPage%>欏?lt;/td>
<td>鍏?lt;%=pageCount%>欏?lt;/td>
<td>
<a href="MyJsp.jsp?showPage=1">棣栭〉</a>
<a href="MyJsp.jsp?showPage=<%=showPage-1%>">涓婁竴欏?lt;/a>
<a href="MyJsp.jsp?showPage=<%=showPage+1%>">涓嬩竴欏?lt;/a>
<a href="MyJsp.jsp?showPage=<%=pageCount%>">灝鵑〉</a>
</td>
<td> 鍏?lt;%=rowCount%>鏉¤褰?</td>
<td>杞埌
<input type="text" name="showPage" size="4"/>
<input type="submit" name="go" value="鎻愪氦"/>
</td>
</tr>
</table>
</form>
<%
con.close() ;
}
catch(Exception e)
{
out.println(e) ;
}
%>
</body>
</html>