Jsp/javabean a page to list data
Last Update:2017-02-28
Source: Internet
Author: User
js| Pagination | Data My first JSP paging code
JSP file: pagedata.jsp//Display data
Javabean:DataConn.java//Connection database
Javabean:GetCount.java//Obtain data record number
Dataconn.java File Contents:
Package aliang_org;
Import java.sql.*;
public class dataconn{
public static Connection Getdataconn () {
Connection conn = null;
try {
Class.forName ("Com.microsoft.jdbc.sqlserver.SQLServerDriver"). newinstance ();
conn = Drivermanager.getconnection ("Jdbc:microsoft:sqlserver://127.0.0.1:1433;databasename=your", "your", "your");
}
catch (Exception ex) {
Ex.printstacktrace ();
}
Return conn;
}
}
GetCount. java file contents:
Package aliang_org;
Import java.sql.*;
Import java.util.*;
Import java.io.*;
public class GetCount {
Private Connection Conn;
Public GetCount () throws exception{
conn = Dataconn.getdataconn ();
}
public int Getcounter () throws exception{
int ret = 0;
Statement stmt = Conn.createstatement ();
ResultSet RSet = Stmt.executequery ("SELECT count (*) from Userinfos");
while (Rset.next ()) {
ret = Rset.getint (1);
}
return ret;
}
}
pagedata.jsp File Contents:
<%@ page contenttype= "text/html; charset=gb2312 "Language=" Java%>
<%@ page import = "aliang_org.*"%>
<%@ page import = "java.util.*"%>
<%@ page import = "java.io.*"%>
<%@ page import = "java.sql.*"%>
<title>
Jsp/javabean a page to list data
</title>
<body bgcolor= "#ffffff" >
<jsp:usebean id= "DataConnection" class= "aliang_org". DataConn "scope=" page/>
<jsp:usebean id= "Getcounter" class= "aliang_org". GetCount "scope=" page/>
<table width= "100%" border= "1" cellpadding= "0" cellspacing= "0" >
<TR align= "center" bgcolor= "#00FFFF" >
<td>id Serial Number </td>
<td> User name </td>
<td> Password </td>
<td> Email </td>
<TD colspan= "2" > Operation options </td>
</tr>
<%
int nowpages;//Current Page
int pages;//Request Pages
int countperpage = 10; Show number of bars per page
int pagecount;//Total Pages
int recordCount = Getcounter.getcounter (); RecordCount as total record number
PageCount = (int) Math.ceil ((RecordCount + countPerPage-1)/countperpage);//Work out the total number of pages
GET Request page
if (Request.getparameter ("pages") = = null) {
pages = 1;
}else{
pages = new Integer (Request.getparameter ("pages")). Intvalue ();
}
Get the current actual page
if (pages > PageCount) {
Nowpages = 1;
}else{
nowpages = pages;
}
Get recordset
Connection conn = Dataconnection.getdataconn ();
ResultSet rs = conn.createstatement (). ExecuteQuery ("select top" + Countperpage + "* from Userinfos where ID is not in" (select) Top ' + (NOWPAGES-1) *countperpage + ID from Userinfos ORDER BY id DESC) ' ORDER BY id desc ');
while (Rs.next ()) {
int ID = Rs.getint ("id");
String username = rs.getstring ("username");
String Password = rs.getstring ("password");
String email = rs.getstring ("email");
%>
<tr>
<TD align= "center" ><%= ID%></td>
<td><%= username%></td>
<td><%= Password%></td>
<td><%= Email%></td>
<TD align= "center" ><a href= "modify.jsp?id=<%= ID%>" > Modify </a></td>
<TD align= "center" ><a href= "delete.jspid=<%= ID%>" > Delete </a></td>
</tr>
<%
}
Close connection
Rs.close ();
Conn.close ();
%>
<form method= "Get" >
<tr align= "Right" >
<TD colspan= "6" > A total of <font color=red><%= recordCount%></font> bar record current <font color=red><% = nowpages%>/<%= PageCount%></font> page
<% if (PageCount > 1) {%>
<% if (pages > 1) {%>
<a href= "" > Home </a>
<%}if (pages < PageCount) {%>
<a href= "? pages=<%= nowpages+1%>" > next page </a>
<%}if (pages!= 1) {%>
<a href= "? pages=<%= nowPages-1%>" > Prev </a>
<%}%>
<a href= "? pages=<%= pagecount%>" > End </a>
<%}%> Jump to
<select name= "pages" onchange= "Javascript:this.form.submit ();" >
<% for (int i=1;i<=pagecount;i++) {%>
<option value= "<%= i%>" <% if (nowpages = i) {%>selected<%}%>><%= I%></option>
<%}%>
</select> page </td>
</tr>
</form>
</table>
©2004 aliang.org Startjava
</body>
Also, don't forget to install the SQL-JDBC driver and copy the C:\Program Files\Microsoft SQL Server Driver for jdbc\lib folder under Web-inf under the Web directory.
Recently began to engage in Java, but also invited you to enlighten email:webmaster@aliang.org prawns, hehe