天天看点

JSP网页分页显示数据库的内容

下面是我的jsp连接mysql数据库的代码,现在有的数据库的条目有1000多个,我想分页显示,每页显示200个,请问怎么改才能实现?

<!doctype html public"-//w3c//dtd html 4.0 transitional//en">

<html>

<head>

<title>alternative splicing ofest</title>

<metaname="generator" content="editplus">

<metaname="author" content="">

<metaname="keywords" content="">

<metaname="description" content="">

</head>

<%@ pageimport="java.sql.*" %>

<% stringvalue=request.getparameter("database");

string abc="aaa";%>

<%if(value.equals("1")){abc="jdbc:odbc:cancer_adrenal_tumor";

}

if(value.equals("2")){abc="jdbc:odbc:cancer_bone_marrow_leukemia";

if(value.equals("3")){abc="jdbc:odbc:cancer_brain_glioma";

%>

<body>

<table border=1  width=800>

 <tr>

   <tdbgcolor="9999ff"><b>est_id</b></td>

   <td bgcolor="9999ff"><b>exon positions</b></td>

    <td bgcolor="9999ff"><b>tissue-specific astranscripts</b></td>

   <tdbgcolor="9999ff"><b>insertion/deletion</b></td>

   <td bgcolor="9999ff"><b>estnumbers</b></td>

   <tdbgcolor="9999ff"><b>geneid</b></td>

 </tr>

<%

class.forname("sun.jdbc.odbc.jdbcodbcdriver");

string url;

 url=abc;

 string username="root";

 string password="4114";

 connectioncon=drivermanager.getconnection(url,username,password);

 statement stmt=con.createstatement();

 resultset rs=stmt.executequery("select *from alter_splicing");

while(rs.next())

{

strings=rs.getstring("sbjct");

   <td><%= rs.getstring("est_id")%></td>

   <td><%= rs.getstring("position")%></td>

 <td><ahref="test2.jsp?aaa=<%=s%>&bbb=<%=abc%>"><%=s%></a></td>

   <td><%= rs.getstring("type")%></td>

   <td><%= rs.getstring("est_numbers")%></td>

 <td><%=rs.getstring("geneid")%></td>

rs.close();

con.close();

</table>

</body>

</html>

问题补充:

请帮忙

最佳答案

以下这段代码请加在你的jsp页面的<body>标签当中

url=abc; //这个url可能有问题的,你自己根据自己数据库的配置再看看

string username="root";

string password="4114";

connectioncon=drivermanager.getconnection(url,username,password);

statementstmt=con.createstatement(resultset.type_scroll_sensitive,resultset.concur_read_only);

resultsetrs=stmt.executequery("select * from alter_splicing");

int totalpage=0;

rs.last();

int lastrow=rs.getrow();

if(lastrow%200==0)

totalpage=lastrow/200;

else

totalpage=lastrow/200+1;

<p>每页显示200行数据,共有<%=totalpage%>页</p>

<form name="form"action="当前页面.jsp"method="post">

<input type="text"name="page" value="" />

<input type="submit"name="submit" value="确定"/>

</form>

 string pagechoosed=request.getparament("page");

 int currentpage=integer.parseint(pagechoosed);

 if(currentpage==null)

  currentpage=1;

 if(currentpage>totalpage)

  currentpage=totalpage;

 int currentrow=(currentpage-1)*200+1;

 rs.absolute(currentrow)

out.println("<tableborder=1>");

   //这里面读数据库里的表的字段,你根据自己的表再看看

   for(int i=0;i<200;i++)

  {

    out.println("<tr>");

    out.println("<td>"+rs.getstring("est_id")+"</td>");

    out.println("<td>"+rs.getstring("position")+"</td>");

    out.println("<td>"+rs.getstring("type")+"</td>");

    out.println("<td>"+rs.getstring("est_numbers")+"</td>");

    out.println("<td>"+rs.getstring("geneid")+"</td>");

    out.println("</tr>");

  }

 out.println("</table>");

 rs.close();

 stmt.close();

 con.close();

方法二:

jsp页面实现分页显示数据库查询内容

对于一个中等以上项目来说,庞大的表信息无法实现在一个jsp页面中预览,分页显示势在必行。本程序主要实现jsp页中查询数据分页显示功能:

string sql=null;

resultset rs=null;

int pagesize=5;

int page=1;

int totalpage=1;

int totalrecord=0;

connection con=splbean.getconnet();

statementstmt=con.createstatement(resultset.type_scroll_insensitive,resultset.concur_read_only);

//算出总行数tatalrecord

sql="select count(*) as recordcount from student";

rs=stmt.executequery(sql);

if(rs.next()) totalrecord=rs.getint("recordcount");

system.out.println(totalrecord);

//算出总页数totalpage

if(totalrecord%pagesize==0) totalpage=totalrecord/pagesize;

else totalpage=(int)math.floor(totalrecord/pagesize)+1;//最后还空余一页情况,math.floor(x):比x小的最大值。

if(totalpage==0) totalpage=1;//数据库中一条数据也没有的处理情况;

//捕捉form表单提交的page元素

if(request.getparameter("page")==null||request.getparameter("page")=="")page=1;

else try{

page=integer.parseint(request.getparameter("page"));

}catch(java.lang.numberformatexception e){

page=1;

//如果page非常规数值处理

if(page>totalpage) page=totalpage;

if(page<1) page=1;

sql="select * from student";

rs.absolute((page-1)*pagesize+1);

resultsetmetadata meta=rs.getmetadata();

int col=meta.getcolumncount();//得到表头有多少列

out.println("<table border='1'>");

//输出表头信息

out.println("<tr>");

for(int i=1;i<col+1;i++)

out.println("<td>");

out.print("列名:");

out.print(meta.getcolumnname(i)+"<br>");

out.print("类型:");

out.print(typetostring(meta.getcolumntype(i)));//检索并返回指定列的sql类型

out.println("</td>");

out.println("<td>更改</td>");

out.println("<td>删除</td>");

out.println("</tr>");

for(int ipage=1;ipage<pagesize+1;ipage++)

out.print("<tr><td>"+rs.getstring(1)+"</td>");

out.print("<td>"+rs.getstring(2)+"</td>");

out.print("<td>"+rs.getstring(3)+"</td>");

out.print("<td>"+rs.getstring(4)+"</td>");

out.print("<td>"+rs.getstring(5)+"</td>");

out.print("<td>"+rs.getstring(6)+"</td>");

out.print("<td><ahref=\"change.jsp?xuehao="+rs.getstring(1)+"\">修改</a></td>");

out.print("<td><ahref=\"del.jsp?xuehao="+rs.getstring(1)+"\">删除</a></td>");

if(!rs.next()) break;

out.println("</table>");

stmt.close();

其中的typetostring()函数的作用是转换得到sql相应的结果类型.如typetostring(1)的得到的结果伟integer