Flex+Java分页

 做了一个Demo,是java+lcds+spring+json.本来想传项目的,可是在csdn只有5M的权限。

服务端的分页代码:package com.ycf; 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中的配置:<bean id="ycfPage" class="com.ycf.YCFPage"> <property name="dataSource"> <ref bean="dataSource"/> </property> </bean>

服务端代码在remoting-config.xml中的配置:<destination id="YCFPage"> <properties> <factory>spring</factory> <source>ycfPage</source> </properties> </destination>

客户端代码:<?xml version="1.0" encoding="utf-8"?> <mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml" width="594" height="38" xmlns:ns1="bridge.*"> <mx:Script> <!--[CDATA[ import mx.controls.Alert; import mx.utils.StringUtil; import mx.collections.ArrayCollection; import com.adobe.serialization.json.JSON; import mx.rpc.events.ResultEvent; //当前页面数据 [Bindable] public var datas:ArrayCollection=new ArrayCollection(); //总记录数 [Bindable] private var counts:int; //总页数 [Bindable] private var pages:int; //页记录数 [Bindable] private var page:int=2; //当前页 [Bindable] private var currpag:int; //sql语句 [Bindable] private var sql:String; //页码 [Bindable] public var db:Array=new Array(); //第一次数据 [Bindable] public function set setData(str:String):void{ var array:Array=JSON.decode(str); if(array[0]!=null){ counts=array[0].counts; pages=array[0].pages; page=array[0].page; currpag=array[0].currpag; sql=array[0].sql; } else return; //pages=Math.ceil(counts/this.page); db.length=0; for(var i:int=1;i<=pages;i++){ db.push("第"+i+"页"); } array.shift(); datas=new ArrayCollection(array); //currpag=1; //addCurr(); inputpage.text=currpag+""; } //页面转换 [Bindable] public function setSData(str:String):void{ /*countdatas=arr; counts=arr.length; pages=Math.ceil(counts/this.page); db.length=0; for(var i:int=1;i<=pages;i++) db.push("第"+i+"页"); //addCurr(); inputpage.text=currpag+"";*/ var array:Array=JSON.decode(str); if(array[0]!=null){ counts=array[0].counts; pages=array[0].pages; page=array[0].page; currpag=array[0].currpag; sql=array[0].sql; } else return; db.length=0; for(var i:int=1;i<=pages;i++){ db.push("第"+i+"页"); } array.shift(); datas=new ArrayCollection(array); inputpage.text=currpag+""; } public function set setPag(page:int):void{ if(page>0) this.page=page; } private function setpag(i:int):void{ if(i==-2) i=currpag-1; if(i==-3) i=currpag+1; if(i==-4) i=pages; currpag=i; if(i<=0){ currpag=1; } else if(i>pages){ currpag=pages; } ycfPage.executeQuery(sql,currpag,page); ycfPage.executeQuery.addEventListener(ResultEvent.RESULT,addCurr); } 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>

你可能感兴趣的:(java,sql,function,String,Flex,button)