这阵子在做一个网站,因为是要从EJB中取数据出来然后再显示出来,用ResultSet做分页比较好做,但是ResultSet又没有序列化不可以在网络上传送。只好把数据放到一个类中然后再放入Vector。
我也是初学都,可能大有初学都做东西都会有相同的问题,所以发上来让大家一起讨论一下。
这种分页方法不科学,要是数据库有上万条记录的话用这种方法的话就...~~~~~~
听说过一种方法就是想取哪页的记录就从数据库中把哪页的记录取出来,不知道哪位大哥可以帮下小弟。有个条件就是要是EJB的实体Bean中实现。谢谢啦
---------------find3.jsp (因为做了很多试验...所以find都到3了...呵呵)
<%@ page contentType="text/html; charset=GBK" import="java.util.*,courseEntityEJB.tempVO,java.io.*" %>
<%//tempVO是存放数据的一个JavaBean,我就是把数据new成一个个的tempVO对向然后存到Vector中去的%>
<html>
<head>
</head>
<jsp:useBean id="bean1" scope="session" class="courseEntityEJB.CourseOperting" />
<jsp:setProperty name="bean1" property="*" />
<%//CourseOperting是从EJB中取数据的一个JavaBean%>
<body bgcolor="#ffffff">
<%
int pageSize = 4;//一页显示的记录条数
int showPage = 1;//初始化正在显示的页码
int rowCount = 0;//要显示的记录条数
int pageCount = 0;//最后一页页码
Vector temp=new Vector();
temp=bean1.selectCourse(bean1.getCourseName());
//得到总记录条数
rowCount = temp.size();
//得到最后一页的页码
pageCount = ((rowCount % pageSize) == 0? (rowCount/pageSize):(rowCount/pageSize) +1);
//该参数toPage为当前页面中表单中传递的参数,用来标记页面的页码
String toPage = request.getParameter("toPage");
if(toPage != null)
{
showPage = Integer.parseInt(toPage);
//如果用户输入的被显示的页码值比总页数大,则显示最后页
if(showPage > pageCount)
{
showPage = pageCount;
}
//用户输入的被显示的页码值比0小或等于0,则显示第1页
else if(showPage <= 0)
{
showPage = 1;
}
}
//记算出从第多少个元素开始向下取记录
int starta=((showPage -1 ) * pageSize);
int a=temp.size();
if(a==0)
{
%>
对不起!没有您要查找的记录!
<%
}
for(int i=0;i<pageSize;i++)
{
if(a==starta)
{
break;
}
tempVO b=(tempVO)temp.elementAt(starta);
//把Vector中元素数据造型为tempVO对像
starta++;
%>
<table>
<tr>
<td> <%=b.getCourseName()%></td>
<td><%=b.getDescription()%></td>
</tr>
</table>
<%
}
%>
<td>
<a href="find3.jsp?toPage=<%=0%>"> 第一页
</a>
</td>
<td>
<a href="find3.jsp?toPage=<%=showPage - 1%>"> 上一页
</a>
</td>
<td>
<a href="find3.jsp?toPage=<%=showPage+1%>"> 下一页
</a>
</td>
<td>
<a href="find3.jsp?toPage=<%=pageCount%>"> 最后一页
</a>
</td>
<td>
共<%=pageCount %>页--当前为<%=showPage %>
</td>
<form action="find3.jsp" method="POST">
<input type="text" name="toPage" value="<%=showPage%>"/>
<input type="submit"/>
</form>
</body>
</html>
------------------------------------CourseOperting.java
package courseEntityEJB;
import java.io.*;
import java.rmi.*;
import java.util.*;
import courseEntityEJB.*;
import javax.ejb.*;
import javax.naming.*;
import javax.rmi.*;
public class CourseOperting{
int code;
String courseName;
String description;
String category;
int coin;
String url;
String submit;
CourseRemote course=null;
CourseRemoteHome coursehome=null;
Vector courseVector=null;
private Vector a=new Vector();
public CourseOperting() {
courseVector=new Vector(1,1);
try {
InitialContext ic = new InitialContext();
coursehome=(CourseRemoteHome)PortableRemoteObject.narrow(ic.lookup("ejb/Course"),CourseRemoteHome.class) ;
} catch (NamingException ex) {
ex.printStackTrace();
}
}
public Vector selectCourse(String courseName){
Vector temp=new Vector();
try{
Collection result=coursehome.findByName(courseName) ;
Iterator i=result.iterator();
while(i.hasNext()){
CourseRemote um = (CourseRemote) i.next();
tempVO uInfo=new tempVO();
uInfo.setCourseName(um.getCourseName());
uInfo.setDescription(um.getDescription());
temp.add(uInfo);
}
}catch(Exception ex){
ex.printStackTrace();
}
return temp;
}
private void readObject(ObjectInputStream ois) throws IOException,
ClassNotFoundException {
ois.defaultReadObject();
}
private void writeObject(ObjectOutputStream oos) throws IOException {
oos.defaultWriteObject();
}
}
--------------------------tempVO.java
package courseEntityEJB;
import java.io.*;
public class tempVO implements Serializable {
private String code;
private String courseName;
private String description;
private void readObject(ObjectInputStream ois) throws IOException,
ClassNotFoundException {
ois.defaultReadObject();
}
private void writeObject(ObjectOutputStream oos) throws IOException {
oos.defaultWriteObject();
}
public void setCode(String code) {
this.code = code;
}
public void setCourseName(String courseName) {
this.courseName = courseName;
}
public void setDescription(String description) {
this.description = description;
}
public String getCode() {
return code;
}
public String getCourseName() {
return courseName;
}
public String getDescription() {
return description;
}
}