从数据库中填充组合框

时间:2014-06-06 00:06:36

标签: java flex arcgis-server

我正在使用flex,Java和sql server进行webmapping应用程序,我正在尝试从数据库填充我的组合框,但我只是得到这样的结果[Object Object] [Object Object] [Object Object] [Object Object ]而不是获得正确的字段值,这是代码,我认为一切都是正确的,但它不起作用!你能帮忙找到我弄乱的东西!!

所以首先这是我的类的代码,它从数据库中选择一个列(Intitule Chapitre)

public class RapportDao {

    public Connection conectar(){
        Connection cn = null;
        String connectionUrl = "jdbc:sqlserver://localhost\\SQLEXPRESS;databaseName=mabase;user=sa;password=sa;";

        try
        {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            cn = DriverManager.getConnection(connectionUrl);

        }
        catch(Exception ex)
        {
            System.out.println("Error : " + ex.getMessage());
        }
        return cn;
    }   
public ArrayList<Rapport> Selection() {


             Connection conn = conectar();  
             ArrayList<Rapport> list = null;

             if (conn!=null){

                 try{
                     Rapport pr = null;
                     String a;
                     list = new ArrayList<Rapport>();
                     String sql = "select IntituleChap from Rapport";
                     Statement st = conn.createStatement();
                     ResultSet rs=st.executeQuery(sql);
                     while (rs.next())
                     {
                         a=rs.getString("IntituleChap");
                         pr  = new Rapport();
                         pr.setIntituleChap(a);
                         list.add(pr);
                     }

             }
                 catch(SQLException e ) {

                     // System.out.print(e.getMessage());
                     System.out.println("Error = " + e.getMessage());
                    }

             }else
             {

             }
             return list;
    }
}

这是我的动作脚本代码

<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
           xmlns:s="library://ns.adobe.com/flex/spark" 
           xmlns:mx="library://ns.adobe.com/flex/mx"
           xmlns:esri="http://www.esri.com/2008/ags"
           minWidth="1000" minHeight="700" applicationComplete="application1_applicationCompleteHandler(event)">

<fx:Script>
            <![CDATA[

    import spark.components.ComboBox;
    private function getTypeprojetResult(event : ResultEvent):void
                {
                    //Alert.show(""+event.result);
                } 

    protected function application1_applicationCompleteHandler(event:FlexEvent):void
            {
                RemoteRapportDao.Selection()

                }
    ]]>

    <fx:Declarations>
            <s:RemoteObject id="RemoteRapportDao" 
                            destination="RapportDaoDest"
                            fault="onFault(event)">
                <s:method name="Selection" result="getTypeprojetResult(event);"/>
            </s:RemoteObject>
        </fx:Declarations>

<s:ComboBox id="cmb" x="10" y="13" width="162" labelField="IntituleChap" dataProvider="{RemoteRapportDao.Selection.lastResult}" />

RemoteRapportDao:是id RemoteObject Selection():我在服务上的方法

任何人都可以帮助我?

2 个答案:

答案 0 :(得分:0)

很可能在正确表示查询结果时遇到问题,因此使用组合框对象不能正确区分labelField。 我建议创建一个代表结果的数据模型,这样不仅可以极大地提高代码的可读性,还可以帮助您解决这些问题。

数据模型的可能示例(仅用于测试目的)。

    package myTopLevelPackage.myPackage
    {
        public class ComboBoxItemDataModel
        {
            public var intituleChap: String;


            public function ComboBoxItemDataModel( o : Object )
            {
                super();
                this.intituleChap = o[ 'IntituleChap' ];
                trace( JSON.encode( o ) ); 
// use this or other suitable method to see actually how Your object looks like, in order to set the object property correctly in the assignment above.
            }
        }
    }

请注意,我已使用大写字母将变量IntituleChap切换为intituleChap。

答案 1 :(得分:0)

它有效,谢谢大家的帮助,我应该使用ArrayCollection而不是arraylist。

public ArrayCollection Selection() {
             Connection conn = conectar();  
                ArrayCollection list =  new ArrayCollection();

             if (conn!=null){

                 try{
                     Rapport pr = null;
                     String a;
                     list = new ArrayList<Rapport>();
                     String sql = "select IntituleChap from Rapport";
                     Statement st = conn.createStatement();
                     ResultSet rs=st.executeQuery(sql);
                     while (rs.next())
                     {
                         a=rs.getString("IntituleChap");
                         pr  = new Rapport();
                         pr.setIntituleChap(a);
                         list.add(pr);
                     }

             }
                 catch(SQLException e ) {

                     // System.out.print(e.getMessage());
                     System.out.println("Error = " + e.getMessage());
                    }

             }else
             {

             }
             return list;
    }

谢谢大家:)