📄 userlist1.jsp
字号:
<%@ page language="java" import="java.sql.*,java.io.*,java.util.*" %>
<%@ page contentType="text/HTML;charset=GB2312"%>
<%
//把经常需要修改的数据放在前边,以方便修改
String username="root"; //数据库用户名
String password="password"; //数据库用户密码
int pagesize=5; //每页显示的数据数量
//JSP程序中半数以上的Exception是NonePointerException,在声明变量时赋予一个
//初始值能缩短调试时间
ResultSet rs = null; //数据库查询结果集
Connection conn=null;
Statement stmt=null;
//下边两个变量是把动态生成的部分集中在一起放入页面
//这样做的好处是当修改页面时就可以直接用Dreamweaver来编辑了
StringBuffer student=new StringBuffer();
StringBuffer pageInfo=new StringBuffer();
try {//注册驱动程序
Class.forName("com.mysql.jdbc.Driver");
}catch(java.lang.ClassNotFoundException e) {
System.err.println("Driver Error " + e.getMessage());
}
//连接数据库并创建Statement对象
String sConnStr = "jdbc:mysql://localhost/test?user="+username+"&password="+password+"&useUnicode=true&characterEncoding=8859_1";
try {
conn = DriverManager.getConnection(sConnStr);
conn.setAutoCommit(true);
//建立Statement对象并设置指针可前后移动
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE ,
ResultSet.CONCUR_READ_ONLY);
}catch(Exception e) {
System.err.println("数据库连接错误: " + e.getMessage());
}
//通过Statement执行SQL语句来获得查询结果
try{//从表sutdent中取出数据
rs=stmt.executeQuery("select * from student");
}catch(SQLException ex){
System.err.println("数据库查询错误: "+ex);
}
//从用户的请求中获取当前页码
//获得需要显示的页,如果用户请求没有页码参数,则默认是第1页
String paramPage=request.getParameter("page");
int currentPage=1;
try{
currentPage=Integer.parseInt(paramPage);
}catch(Exception e){
currentPage=1;
}
//将要显示的当前页的数据记录放入student中
rs.last(); //把指针置底
int totaluser=rs.getRow(); //获得结果数量
//计算出总页数
int pagecount=(int)Math.ceil((float)totaluser/(float)pagesize);
int i=1;
rs.absolute((currentPage-1) * pagesize + 1); //把指针放到要显示的第一个数据
//把结果放进变量student
while(i<=pagesize && !rs.isAfterLast()){
student.append("<tr bgcolor=#ffffff>\n");
student.append("<td align=center>"+rs.getString("Sno")+"</td>");
student.append("<td align=center>"+rs.getString("Sname")+"</td>");
student.append("<td align=center>"+rs.getString("Sage")+"</td>");
student.append("<td align=center>"+rs.getString("Sdept")+"</td>");
student.append("</tr>");
if(!rs.next()) //当到达最后一个记录时退出循环
break;
i++;
}
//关闭数据库连接
try{
rs.close(); //关闭结果集对象
stmt.close(); //关闭Statement对象
conn.close(); //关闭数据库连接对象
}catch(SQLException e){
System.err.println(e);
}
//将要显示页码信息放入pageInfo
//把显示翻页的字段放进变量pageInfo
pageInfo.append("第"+currentPage+"页 共"+pagecount+"页,共"+totaluser+"个学生 ");
if(currentPage>1) //在当前页大于1时有向前翻页的连接,否则没有
pageInfo.append(" <a href='userlist.jsp?page="+(currentPage-1)+"'><< 上一页</a>");
else
pageInfo.append("<< 上一页");
if(currentPage<pagecount) //在当前页小于总页数时有向前翻页的连接,否则没有
pageInfo.append(" <a href='userlist.jsp?page="+(currentPage+1)+"'>下一页 >></a>");
else
pageInfo.append("下一页 >>");
//显示结果
%>
<html>
<head>
</head>
<body>
<table width="80%" border="0" align="center" cellpadding=5 cellspacing=1 bgcolor=#000000>
<tr align="center" bgcolor=#cccccc>
<td width="5%">学号</td>
<td width="5%">姓名</td>
<td width="5%">年龄</a></td>
<td width="5%">所在系</td>
</tr>
<%=student%>
</table>
<table width="80%" border="0" align="center">
<tr>
<td align=right><%=pageInfo%>
</td>
</tr>
</table>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -