1.我的数据库名叫aaaQ密码是123
2.定数据库中有数?q是查询功能+分页
创徏数据库sql
CREATE TABLE users (
username varchar2(100),
department varchar2(100),
headship varchar2(100),
)
cL在src下的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);//可滚动查询数据的l果?br />
request.setCharacterEncoding("GB2312");
rs = stmt.executeQuery("select * from users") ;
rs.last(); //让游标到表中的最后一?br />
int rowCount = rs.getRow(); //获取记录L.
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() 游标是否在最后一行之后说明后面已l没记录
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>当前W?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%>">N</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>