天天看點

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