当前页面: 开发资料首页 → JSP 专题 → JSP实现留言板的JavaBean类和分页显示的源码
摘要: JSP实现留言板的JavaBean类和分页显示的源码
//留言bean
package userinfobean;
/**
*
Title:
Description:
Copyright: Copyright (c) 2004
Company:
public class memoBean {
private String id;//1.用户名
private String email;//2.密码
private String content;//3.留言
public memoBean() {
}
//1.用户名
public void setId(String s){
this.id=s;
}
//2.密码
public void setEmail(String s){
this.email=s;
}
//3.留言
public void setContent(String s){
this.content=s;
}
//1.用户名
public String getId( ){
return this.id;
}
//2.密码
public String getEmail(){
return this.email;
}
//3.留言
public String getContent(){
return this.content;
}
}
//// 用于数据库操作的bean
package userinfobean;
import userinfobean.*;
import java.io.*;
import java.util.*;
import java.sql.*;
public class databaseBean{
private String dbName;
private String dbUser;
private String dbPass;
private Vector memoVector;
Connection connection;
public databaseBean(){
dbName=new String("×××××××");
dbUser=new String("××××××");
dbPass=new String("×××××××");
String connectionUrl="jdbc:mysql://localhost/"+dbName;
try{
Class.forName("org.gjt.mm.mysql.Driver");
connection=DriverManager.getConnection(connectionUrl,dbUser,dbPass);
}
catch(Exception e){
System.out.println(e.toString());
}
}
public void dbQueryMemos(){
try{
memoVector=new Vector();
Statement stmt=connection.createStatement();
ResultSet rs=stmt.executeQuery("select * from memo;");
while(rs.next()){
memoBean temp=new memoBean();
temp.setId(rs.getString(2));
temp.setEmail(rs.getString(3));
temp.setContent(rs.getString(4));
memoVector.add(temp);
}
}
catch(SQLException e){
System.out.println(e.toString());
}
}
public void setDbName(String s){
this.dbName=s;
}
public void setDbUser(String s){
this.dbUser=s;
}
public void setDbPass(String s){
this.dbPass=s;
}
public String getDbName(){
return this.dbName;
}
public String getDbUser(){
return this.dbUser;
}
public String getDbPass(){
return this.dbPass;
}
public Vector getMemoVector(){
return this.memoVector;
}
}
///以下是网页源码
HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%@ page language="java" import="java.util.*" %>
<%@ page import="java.text.*" %>
<%@ page import="userinfobean.*" %>
<%!
int totalPages;
int countPerPage=3;
int totalCount;
int currentPage=0;
int currentStart;
%>
<%
memoQuery.dbQueryMemos();
Vector vt=memoQuery.getMemoVector();
totalCount=vt.size();
totalPages=totalCount/countPerPage;
if(totalCount%countPerPage>0)totalPages=totalPages+1;
if(request.getParameter("page")==null)
{
currentPage=1;
}
else
{
currentPage=Integer.parseInt(request.getParameter("page"));
if(currentPage>totalPages)currentPage=totalPages;
if(currentPage<1)currentPage=1;
}
currentStart=totalCount-1-countPerPage * (currentPage-1);
%>
<head>