100分在线苦求:分页标签
我现在发觉公司哪个分页标签很是不好用,并且不了解他内部到底是怎么实现的,所以在我用的时候很是麻烦,每次都要去问他们,有时人家忙还不给说,很是郁闷. 现在我正在写一个,但是在从数据库里面取数据的时候有点问题.有没有那位仁兄做个这方面例子,给小弟参考参考
[解决办法]
有一个专门分页的开源项目,是一个包,非常好用,外国人写的,你在网上找得都是垃圾,不建议参考
[解决办法]
数据量不大的话可以用个开源表格 extremecomponents
自动分页和各种导出
[解决办法]
<%@ page contentType="text/html;charset=GBK"%>
<%@ page import="java.sql.*"%>
<html>
<title>分页显示</title>
<body>
<%! int pageSize = 5;
int pageCount = 0;
%>
<%
Connection con;
String DatabaseDriver = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
String CnnStr = "jdbc:microsoft:sqlserver://localhost:1433;databasename=数据库名";
try {
Class.forName(DatabaseDriver);
con = DriverManager.getConnection(CnnStr, "sa", "");
Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
ResultSet rs = stmt.executeQuery("select * from hunqingzixun order by id desc");
rs.last();
int rowCount = rs.getRow();
pageCount = (rowCount % pageSize == 0) ? (rowCount / pageSize ) : (rowCount / pageSize +1);
int showPage = 1;
%>
<%
String goToPage = request.getParameter("showPage");
if (goToPage == null){
goToPage = "1";
}
try{
showPage = Integer.parseInt(goToPage);
}
catch (NumberFormatException ex){
showPage = 1;
}
if(showPage <=1){
showPage = 1;
}
else if(showPage >= pageCount){
showPage = pageCount;
}
int posion = (showPage -1 ) * pageSize + 1;
rs.absolute(posion);
%>
<table border="1" cellspacing="0" cellpadding="0">
<tr>
<th>商品编号</th>
<th>商品英文名称</th>
<th>商品中文名称</th>
</tr>
<%
int i =0;
while(i<pageSize && !rs.isAfterLast()){
%>
<tr>
<td><%=rs.getString(1)%></td>
<td><%=rs.getString(2)%></td>
<td><%=rs.getString(3)%></td>
</tr>
<%rs.next();i++;}%>
</table>
<table width="624">
<form action="fenye.jsp" method="post">
<tr>
<td>
<%if(showPage < pageCount){%>
<a href="fenye.jsp?showPage=<%=showPage+1%>">[下一页]</a>
<%}%>
</td>
<td><%if(showPage > 1){%>
<a href="fenye.jsp?showPage=<%=showPage-1%>">[上一页]</a>
<%}%>
</td>
<td> 共<%=pageCount%>页 </td>
<td> 第<%=showPage%>页 </td>
<td> <a href="fenye.jsp?showPage=1">『首页』</a> </td>
<td> <a href="fenye.jsp?showPage=<%=pageCount%>">『尾页』</a> </td>
<td> <%=rowCount%> </td>
<td>
转到<input type="text" name="showPage" size="4"></input>
<input type="submit" name="go" value="提交"></input>
</td>
</tr>
</form>
</table>
<%
con.close();
} catch (ClassNotFoundException e1) {out.print(e1.getMessage());}
catch(SQLException e2){ out.print(e2.getMessage());}
%>
</body>
</html>
好象我并没看明白你说的意思~
这个就是分页的一个功能,不知道没用MVC模式你能看明白不!
但这个分页肯定好使!自己改成MVC的吧 !
我也有点懒得写啦
[解决办法]
<%@ page contentType="text/html;charset=GBK"%>
<%@ page import="java.sql.*"%>
<html>
<title>分页显示</title>
<body>
<%! int pageSize = 5;
int pageCount = 0;
%>
<%
Connection con;
String DatabaseDriver = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
String CnnStr = "jdbc:microsoft:sqlserver://localhost:1433;databasename=数据库名";
try {
Class.forName(DatabaseDriver);
con = DriverManager.getConnection(CnnStr, "sa", "");
Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
ResultSet rs = stmt.executeQuery("select * from hunqingzixun order by id desc");
rs.last();
int rowCount = rs.getRow();
pageCount = (rowCount % pageSize == 0) ? (rowCount / pageSize ) : (rowCount / pageSize +1);
int showPage = 1;
%>
<%
String goToPage = request.getParameter("showPage");
if (goToPage == null){
goToPage = "1";
}
try{
showPage = Integer.parseInt(goToPage);
}
catch (NumberFormatException ex){
showPage = 1;
}
if(showPage <=1){
showPage = 1;
}
else if(showPage >= pageCount){
showPage = pageCount;
}
int posion = (showPage -1 ) * pageSize + 1;
rs.absolute(posion);
%>
<table border="1" cellspacing="0" cellpadding="0">
<tr>
<th>商品编号</th>
<th>商品英文名称</th>
<th>商品中文名称</th>
</tr>
<%
int i =0;
while(i<pageSize && !rs.isAfterLast()){
%>
<tr>
<td><%=rs.getString(1)%></td>
<td><%=rs.getString(2)%></td>
<td><%=rs.getString(3)%></td>
</tr>
<%rs.next();i++;}%>
</table>
<table width="624">
<form action="fenye.jsp" method="post">
<tr>
<td>
<%if(showPage < pageCount){%>
<a href="fenye.jsp?showPage=<%=showPage+1%>">[下一页]</a>
<%}%>
</td>
<td><%if(showPage > 1){%>
<a href="fenye.jsp?showPage=<%=showPage-1%>">[上一页]</a>
<%}%>
</td>
<td> 共<%=pageCount%>页 </td>
<td> 第<%=showPage%>页 </td>
<td> <a href="fenye.jsp?showPage=1">『首页』</a> </td>
<td> <a href="fenye.jsp?showPage=<%=pageCount%>">『尾页』</a> </td>
<td> <%=rowCount%> </td>
<td>
转到<input type="text" name="showPage" size="4"></input>
<input type="submit" name="go" value="提交"></input>
</td>
</tr>
</form>
</table>
<%
con.close();
} catch (ClassNotFoundException e1) {out.print(e1.getMessage());}
catch(SQLException e2){ out.print(e2.getMessage());}
%>
</body>
</html>
好象我并没看明白你说的意思~
这个就是分页的一个功能,不知道没用MVC模式你能看明白不!
但这个分页肯定好使!自己改成MVC的吧 !
我也有点懒得写啦
[解决办法]
用displaytag-1.0.jar分页标签来处理
[解决办法]
package ch13;
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
public class MySQLNamesTest extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head>");
out.println("<title>MYSQL Test</title>");
out.println("</head>");
out.println("<h1>A list of names from a <br>MySQL table</h1>");
out.println("<table border=\"1\" cellpadding=\"5\""
+ "cellspacing=\"0\" width=\"300\">");
out.println("<tr><td><b>First Name</b></td><td><b>"
+ "Last Name</b></td></tr>");
/*
* WARNING! The following code demonstrates connnedting to a MySQL
* database. In a real world implementation youi would want to use
* connection pooling and also specify the connection information as
* initialization parameters. Connecting to the server on each
* servlet request can result in delays (although the mm driver does
* establish a connection quite quickly).
*/
String connectionURL = "jdbc:mysql://localhost:3306/test";
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
try {
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
connection = DriverManager.getConnection(connectionURL, "servlet",
"zhou");
statement = connection.createStatement();
resultSet = statement
.executeQuery("select FirstName,LastName from Names"
+ "order by LastName,FirstName");
while (resultSet.next()) {
out.println("<tr><td>" + resultSet.getString("FirstName")
+ "</td><td>" + resultSet.getString("LastName")
+ "</td></tr>");
}
if (resultSet != null) {
resultSet.close();
}
} catch (ClassNotFoundException e) {
System.err.println("Couldn't find the mm" + "database driver:"
+ e.getMessage());
} catch (InstantiationException e) {
System.err.println(e.getMessage());
} catch (IllegalAccessException e) {
System.err.println(e.getMessage());
} catch (SQLException e) {
System.err.println("SQL Problem :" + e.getMessage());
System.err.println("SQL State :" + e.getSQLState());
System.err.println("Vendor Error:" + e.getErrorCode());
} finally {
try {
if (connection != null) {
connection.close();
}
} catch (SQLException e) {
System.err.println(e.getMessage());
}
}
out.println("<body>");
out.println("</body>");
out.println("</html>");
}
}