Jsp/javabean a page to list data

Source: Internet
Author: User
Tags microsoft sql server modify sql server driver stmt
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" >
&LT;TR align= "center" bgcolor= "#00FFFF" >
<td>id Serial Number </td>
<td> User name </td>
<td> Password </td>
<td> Email </td>
&LT;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>
&LT;TD align= "center" ><%= ID%></td>
<td><%= username%></td>
<td><%= Password%></td>
<td><%= Email%></td>
&LT;TD align= "center" ><a href= "modify.jsp?id=<%= ID%>" > Modify </a></td>
&LT;TD align= "center" ><a href= "delete.jspid=<%= ID%>" > Delete </a></td>
</tr>
<%
}
Close connection
Rs.close ();
Conn.close ();
%>
<form method= "Get" >
<tr align= "Right" >
&LT;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




Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.