CSDN博客

img brucejia

分页类和页面,(MYSQL,其他数据库需更改SQL)

发表于2004/9/17 16:56:00  473人阅读

类DataList

 

/*

* Created on 2004-9-17

*

* To change the template for this generated file go to

* Window>Preferences>Java>Code Generation>Code and Comments

*/

package com.jia.database;

import java.sql.Connection;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;

import java.util.ArrayList;

import java.util.logging.Logger;

/**

* @author Administrator

*

* To change the template for this generated type comment go to

* Window>Preferences>Java>Code Generation>Code and Comments

*/

 

 

public class DataList {

private static String sql_teacher_count = "select count(*) from teacher";

private static ArrayList arr_teacher = new ArrayList();

private static int totalCount = 0;

 

/*取出总纪录数*/

 

public static int getTeacherCount() {

Connection conn = ConnectDB.ConnMysqlbyJDBC();

Statement stmt = null;

try {

stmt = conn.createStatement();

ResultSet rs = stmt.executeQuery(sql_teacher_count);

totalCount = rs.getInt(1);

} catch (SQLException e) {

Logger log = Logger.getLogger("");

log.severe("取出表数据错误" + e.getMessage());

// TODO: handle exception

} finally {

try {

stmt.close();

conn.close();

} catch (Exception e) {

}

}

return totalCount;

}

/*取出分页纪录*/

public static ArrayList getTeacherList(int p1, int p2) {

Connection conn = ConnectDB.ConnMysqlbyJDBC();

Statement stmt = null;

try {

arr_teacher.clear();

stmt = conn.createStatement();

ResultSet rs =

stmt.executeQuery(

"SELECT id,name,address FROM teacher limit "

+ p1

+ ","

+ p2

+ "");

while (rs.next()) {

Teacher teacher = new Teacher();

teacher.setId(rs.getString(1));

teacher.setName(rs.getString(2));

teacher.setAddress(rs.getString(3));

arr_teacher.add(teacher);

}

} catch (SQLException e) {

// TODO: handle exception

Logger log = Logger.getLogger("");

log.severe("取出表数据错误" + e.getMessage());

} finally {

try {

stmt.close();

conn.close();

} catch (Exception e) {

}

}

return arr_teacher;

}

}

标准javaBean,teacher表

 

/*

* Created on 2004-9-17

*

* To change the template for this generated file go to

* Window>Preferences>Java>Code Generation>Code and Comments

*/

package com.jia.database;

/**

* @author Administrator

*

* To change the template for this generated type comment go to

* Window>Preferences>Java>Code Generation>Code and Comments

*/

public class Teacher {

 

private String name="";

private String address="";

private String id="";

 

/**

* @return

*/

public String getAddress() {

return address;

}

/**

* @return

*/

public String getName() {

return name;

}

/**

* @param string

*/

public void setAddress(String string) {

address = string;

}

/**

* @param string

*/

public void setName(String string) {

name = string;

}

/**

* @return

*/

public String getId() {

return id;

}

/**

* @param string

*/

public void setId(String string) {

id = string;

}

}

显示页面default.jsp

<%@ page language="java" errorPage="error.html" pageEncoding="GB2312" %>

<%@ page import="java.util.*"%>

<%@ page import="com.jia.database.Teacher"%>

<%@ page import="com.jia.database.DataList"%>

<!DOCTYPE HTML PUBLIC "-//w3c//dtd html 4.0 transitional//en">

<html>

<head>

<title>Lomboz JSP</title>

<link href="/WebStation/css/style.css" rel="stylesheet" type="text/css">

</head>

<body bgcolor="#FFFFFF">

<!---------------------可抽取为单独的jsf文件---------------------------->

<%

int rowNum=DataList.getTeacherCount();

int pageSize=5;

int pageNum=rowNum%pageSize==0?(rowNum/pageSize) : (rowNum/pageSize+1);

int curPage=1;

if(request.getParameter("curPage")!=null)

curPage=Integer.parseInt(request.getParameter("curPage"));

%>

<!---------------------------------------------------------------------->

<%

ArrayList al=DataList.getTeacherList((curPage-1)*pageSize,pageSize);

Teacher teacher=new Teacher();

Iterator it =al.listIterator();

while (it.hasNext()) {

teacher=(Teacher)it.next();

out.println(teacher.getId()+teacher.getName()+teacher.getAddress()+"<br>");

}

%>

 

<!---------------------可抽取为单独的jsf文件---------------------------->

<%=pageNum%>页 第<%=curPage%>

<%if(curPage-1>0){%>

<a href="default.jsp?curPage=<%=curPage-1%>"><<<<</a>

<%}%>

<%if(curPage+1<=pageNum){%>

<a href="default.jsp?curPage=<%=curPage+1%>">>>>></a>

<%}%>

<!---------------------------------------------------------------------->

<form method="post" action="/WebStation/LoginServlet">

<center>

用户:<input type="text" name="userName">

密码:<input type="text" name="password">

验证码:<input type="text" name="veritycode" size=4 maxlength=4><script>document.write("<img border=0 src='/WebStation/verifyImg'>");</script>

<input type="submit" value="登陆">

</center>

</form>

</body>

</html>

0 0

相关博文

我的热门文章

img
取 消
img