1

i tried below code in which i want to fetch all values when WHERE condition satisfied..and my M S Access databse contains NULL values also but when i m fetching items from database into combobox i dont want NULL values in my combo...nd below code fetches null values also..

code i tried:

 if(e.getSource()==cmb_dest)
    {
             cmb_route.removeAll();

             try
            {
               Connection con;
               DriverManager.registerDriver(new sun.jdbc.odbc.JdbcOdbcDriver());
               Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
               con = DriverManager.getConnection("jdbc:odbc:dsnproj","","");
               Statement s = con.createStatement();
            // String qry="select c_id,p_category from product";
               String qry2="select * from trans_dest where 
               dest='"+cmb_dest.getSelectedItem()+"'";    
               System.out.println(qry2); 
               ResultSet rs=s.executeQuery(qry2);
             //ResultSet rs1=s1.executeQuery(qry2);
               while(rs.next())
               {                           
                       cmb_route.add(rs.getString("rout1"));
                       cmb_route.add(rs.getString("rout2"));
                       cmb_route.add(rs.getString("rout3"));
                       cmb_route.add(rs.getString("rout4"));
               }   
            }
             catch(Exception re)
             {
                 System.out.println(re.getMessage());
             }
        }

there is no error but combobox contains blank items also....

4

2 に答える 2

2

あなたがしたいことをするために、4つのフィールドを持つオブジェクトを構築する必要があります

           while(rs.next())
           {                           
                   cmb_route.add(rs.getString("rout1"));
                   cmb_route.add(rs.getString("rout2"));
                   cmb_route.add(rs.getString("rout3"));
                   cmb_route.add(rs.getString("rout4"));
           }

これの代わりにあなたがする必要があります

           your_object vo = null;
           while(rs.next())
           {       
                   vo = new your_object();
                   vo.setRout1(rs.getString("rout1"));
                   vo.setRout2(rs.getString("rout2"));
                   vo.setRout3(rs.getString("rout3"));
                   vo.setRout4(rs.getString("rout4"));
                   cmb_route.add(vo);

           }  
于 2013-03-07T08:31:02.117 に答える
0

getString fromは、列の値が SQL の場合にResultSet返します。したがって、返された値をJavaに対してチェックし、そうでない場合にのみリストに追加しますnullNULLnull

String rout1 = rs.getString("rout1");
if(rout1 != null) {
    cmb_route.add(rout1);
}
于 2013-03-07T08:36:17.477 に答える