日期:2014-05-16  浏览次数:20362 次

不妨看看我用dwr和json完成简单分页-jsp dwr分页(mssqlserver2005)

以前用dwr写的分页,感觉稍微有点麻烦,简单,可以在csdn的http://download.csdn.net/source/1413703下载,sqlser2000的数据库。

?

说明:我对json不熟悉,也不是陌生的那种,就是没怎么使用过,只是知道一点点。如果你也是这样的话,不妨你先看看

这里我的上一篇,我还是觉得挺简单的,代码没怎么细化,总体上讲简单,附上一下文件的源文件。

?

1、分页毫无疑问肯定有数据库链接类,注意,这里是mssql2005的数据库

package com.db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

/**
 * @Info 数据库链接管理类
 * @Email wwwchendonglisahao@163.com
 * @QQ 271069593
 */
public class DBManager {

	private static final String URL = "jdbc:sqlserver://localhost:1433;databasename=Struts";
	
	private static final String USERNAME = "sa";
	
	private static final String PASSWORD = "";
	
	public static Connection getConnection(){

		Connection con = null;
		try {
			Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
			con = DriverManager.getConnection(URL, USERNAME, PASSWORD);
		} catch (Exception e) {
			System.out.println("DBManager.getConnection()" + e.getMessage());
			e.printStackTrace();
		}
		
		return con;
	}
	
	public static void Close(ResultSet rs ,PreparedStatement pst,Connection conn){
		
		try {
			if (rs != null)
				rs.close();
			if (pst != null)
				pst.close();
			if (conn != null)
				conn.close();
		} catch (Exception e) {
			System.out.println("DBManager.Close()" + e.getMessage());
			e.printStackTrace();
		}		
		
	}
	
}

?

2、数据库表结构信息

use struts

--drop table account
--新建表
create table account(
  id int identity(1,1) primary key,
  username varchar(10) default '',
  age int ,
  sex varchar(2)
)

--向表插入100条数据
declare @age int
declare @sex varchar(2)
set @age = 1
while(@age<=100)
begin
   if(@age%3=0)
   begin
     set @sex = '男'
   end
   else 
     set @sex = '女'
   insert into account values('冬冬',@age,@sex);
   set @age = @age + 1
end

--查询表数据
select * from account

?

3、dwr环境配置

(1)、web.xml配置

<servlet>
    <servlet-name>dwr-remote</servlet-name>
    <servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class>
    <init-param>
      <param-name>debug</param-name>
      <param-value>true</param-value>
    </init-param>
</servlet>
<servlet-mapping>
    <servlet-name>dwr-remote</servlet-name>
    <url-pattern>/dwr/*</url-pattern>
</servlet-mapping>

?(2)、dwr.xml

?

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 3.0//EN" "dwr30.dtd">

<dwr>
	<allow>
		<create creator="new" javascript="p">
			<param name="class" value="com.dao.AnalysePage"></param>
		</create>
	</allow>
</dwr>

?

说明:我是把dwr3.0的dtd文件下载下来了,所以上面的这一行<!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 3.0//EN" "dwr30.dtd">红字部分请注意。

?

4、分页处理类

package com.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;

import com.db.DBManager;

/**
 * 处理完成分页,让dwr在页面上调用处理
 *
 */
public class AnalysePage {

	private Connection conn = null;
	private PreparedStatement pst = null;
	private ResultSet rs = null;
	
	/**
	 * 处理分页的方法
	 * 默认为每页显示10条数据
	 * @param page 页数
	 */
	public ArrayList<String> loadAll(int page){
		
		ArrayList<String> list = null;
		int pageSize = 10;
		
		String sql = "select top " + pageSize + " *,(select count(0) from Account) as datasizes from Account where id not in (select top " + (pageSize * (page-1)) + " id from Account)";
		System.out.println(sql);
		conn = DBManager.getConnection();
		try