做了一个Demo,是java+lcds+spring+json.本来想传项目的,可是在csdn只有5M的权限。
服务端的分页代码:
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import javax.sql.DataSource;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class YCFPage {
private DataSource dataSource;
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}
/**
* 获得数据源
* @return
* 返回数据源
*/
public DataSource getDataSource() {
return dataSource;
}
/**
* 获得连接池连接
* @return
* 返回连接对象
*/
public Connection getConn() {
Connection conn = null;
try {
conn=getDataSource().getConnection();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}
/**
* 返回JSON数据
* 第一条为页面对象
* @param sql
* 如select * from table
* @param currpag
* 当前页
* @param page
* 每页显示数目
* @return
* 返回字符串
*/
public String executeQuery(String sql,int currpag,int page){
String str=null;
JSONArray jsonEmployeeArray = new JSONArray();
int counts=executeCount(sql);
int pages=(counts-1)/page+1;
try {
pages = (counts-1)/page+1;
} catch (Exception ex) {
pages = 0;
}
if(currpag>pages)
currpag=pages;
else if(currpag<=0)
currpag=1;
String ssql=sql;
sql="Select * from (Select t.*,rownum rn from (" + sql
+ " ) t where " + "rownum <= " + page + "*" + currpag
+ ")" + " t where t.rn >=(" + currpag+ "-1)* " + page
+ "+1";
JSONObject jsObject = new JSONObject();
Connection conn = this.getConn();
Statement stm = null;
ResultSet rs = null;
ResultSetMetaData rms = null;
try {
jsObject.put("page", page);
jsObject.put("sql", ssql);
jsObject.put("currpag", currpag);
jsObject.put("counts", counts);
jsObject.put("pages", pages);
jsonEmployeeArray.put(jsObject);
stm = conn.createStatement();
rs = stm.executeQuery(sql);
rms = rs.getMetaData();
while (rs.next()) {
JSONObject jsonEmployee = new JSONObject();
for (int i = 1; i <= rms.getColumnCount(); i++) {
String field = rms.getColumnName(i).toLowerCase();
jsonEmployee.put(field, rs.getString(field));
}
jsonEmployeeArray.put(jsonEmployee);
}
} catch (SQLException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
rs.close();
stm.close();
conn.close();
} catch (SQLException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
}
return jsonEmployeeArray.toString();
}
/**
* 统计sql记录数
* @param sql
* 如select * from table
* @return
* 返回记录数
*/
public int executeCount(String sql) {
int n = 0;
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
conn = this.getConn();
stmt = conn.createStatement(
java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE,
java.sql.ResultSet.CONCUR_READ_ONLY);
rs = stmt.executeQuery(sql);
rs.last();
n = rs.getRow();
} catch (Exception ex) {
ex.printStackTrace();
} finally {
try {
rs.close();
stmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return n;
}
/**
* 返回第一页数据
* @param sql
* 如select * from table
* @param page
* 每页记录数
* @return
* JSON数据
*/
public String firstExecute(String sql,int page){
return executeQuery(sql, 1, page);
}
}
服务端代码在applicationContext.xml中的配置:
服务端代码在remoting-config.xml中的配置:
客户端代码:
}
private function goPage():void{
var pp:String=StringUtil.trim(inputpage.text);
var i:int;
if(pp.length==0)
return;
try{
i=Number(pp);
}
catch(e:Error){
inputpage.text="";
return;
}
setpag(i);
}
private function addCurr(e:ResultEvent):void{
setSData(e.result.toString());
}
]]-->
</mx:Script>
<mx:HBox x="15" y="4" width="569">
<mx:Button label="首页" fontSize="12" id="first" click="setpag(1)"/>
<mx:Button label="上一页" fontSize="12" id="up" click="setpag(-2)"/>
<mx:Button label="下一页" fontSize="12" id="down" click="setpag(-3)"/>
<mx:Button label="尾页" id="end" fontSize="12" click="setpag(-4)"/>
<mx:ComboBox id="comb" width="58" editable="false" change="setpag(comb.selectedIndex+1)" dataProvider="{db}"></mx:ComboBox>
<mx:TextInput width="36" id="inputpage" />
<mx:Button label="GO" click="goPage()"/>
<mx:Label width="138" height="21" text="当前{currpag}/{pages};记录数:{counts}" fontSize="12"/>
</mx:HBox>
<mx:RemoteObject id="ycfPage" destination="YCFPage"></mx:RemoteObject>
</mx:Canvas>