只是第一个ArrayList <string>工作</string>

时间:2014-10-25 12:55:49

标签: java mysql arraylist

所以我创建了两个相同的方法,并返回主席ID和预订日期:

public ArrayList<String> GetChair(String Class) throws SQLException{
      ArrayList<String> arr = new ArrayList<String>(20);
      openconnection();
      String querychair = "SELECT * FROM hema.flight_usa WHERE free = 1 and class='"+Class+"'";
      Statement stm=(Statement) con.createStatement();

    ResultSet rs;
   rs = stm.executeQuery(querychair);

   while(rs.next())
   {
      arr.add(rs.getString("chair_id"));



   }
   return arr;
  }

  //---------------- The Second One ----------------------------------------

  public ArrayList<String> GetDate(String Class) throws SQLException{

     ArrayList<String> arr2 = new ArrayList<String>(20);

      openconnection();
      String querydate = "SELECT * FROM hema.flight_usa WHERE free = 1 and class='"+Class+"'";
      Statement stm=(Statement) con.createStatement();

    ResultSet rst;
   rst = stm.executeQuery(querydate); 

      while(rst.next())
   {
      arr2.add(rst.getString("res_date"));



   }
   return arr2;

  }

然后我调用这两个方法从表中获取数据 问题是我只得到第一个被调用的方法:

 private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         

    try {
        String Class=Class1Text.getText();
        Model model =new Model();


        ArrayList<String> arr1=new ArrayList<String>(20);

        arr1=model.GetDate(Class);


        jTextField12.setText(arr1.get(0));
        jTextField13.setText(arr1.get(1));
        jTextField14.setText(arr1.get(2));
        jTextField15.setText(arr1.get(3));
        jTextField16.setText(arr1.get(4));
        jTextField17.setText(arr1.get(5));
        jTextField18.setText(arr1.get(6));
        jTextField19.setText(arr1.get(7));
        jTextField20.setText(arr1.get(8));
        jTextField21.setText(arr1.get(9));


        ArrayList<String> arr=new ArrayList<String>(20);
           arr =     model.GetChair(Class);

        jTextField2.setText(arr.get(0));
        jTextField3.setText(arr.get(1));
        jTextField4.setText(arr.get(2));
        jTextField5.setText(arr.get(3));
        jTextField6.setText(arr.get(4));
        jTextField7.setText(arr.get(5));
        jTextField8.setText(arr.get(6));
        jTextField9.setText(arr.get(7));
        jTextField10.setText(arr.get(8));
        jTextField11.setText(arr.get(9));



    } catch (SQLException ex) {
        Logger.getLogger(Reservation.class.getName()).log(Level.SEVERE, null, ex);
    }



}                              

这样就可以完成GetDate方法了

和他一起给我这个错误:

Exception in thread "AWT-EventQueue-0" java.lang.IndexOutOfBoundsException: Index: 3, Size: 3 at java.util.ArrayList.rangeCheck(ArrayList.java:638) at java.util.ArrayList.get(ArrayList.java:414) at View.Reservation.jButton1ActionPerformed(Reservation.java:293) at View.Reservation.access$000(Reservation.java:16) at View.Reservation$1.actionPerformed(Reservation.java:84) at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022) at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2346) at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402) at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259) at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252) at java.awt.Component.processMouseEvent(Component.java:6527) at javax.swing.JComponent.processMouseEvent(JComponent.java:3321) at java.awt.Component.processEvent(Component.java:6292) at java.awt.Container.processEvent(Container.java:2234) at java.awt.Component.dispatchEventImpl(Component.java:4883) at java.awt.Container.dispatchEventImpl(Container.java:2292) at java.awt.Component.dispatchEvent(Component.java:4705) at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4898) at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4533) at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4462) at java.awt.Container.dispatchEventImpl(Container.java:2278) at java.awt.Window.dispatchEventImpl(Window.java:2739) at java.awt.Component.dispatchEvent(Component.java:4705) at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:746) at java.awt.EventQueue.access$400(EventQueue.java:97) at java.awt.EventQueue$3.run(EventQueue.java:697) at java.awt.EventQueue$3.run(EventQueue.java:691) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:86) at java.awt.EventQueue$4.run(EventQueue.java:719) at java.awt.EventQueue$4.run(EventQueue.java:717) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75) at java.awt.EventQueue.dispatchEvent(EventQueue.java:716) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93) at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

2 个答案:

答案 0 :(得分:0)

抛出IndexOutOfBoundsException以指示索引超出范围。在第二个结果集中,您调用了10个结果,结果可能只有3行。

答案 1 :(得分:0)

在获取详细信息之前检查列表的大小,

if(arr1.size()>9){
        jTextField12.setText(arr1.get(0));
        jTextField13.setText(arr1.get(1));
        jTextField14.setText(arr1.get(2));
        jTextField15.setText(arr1.get(3));
        jTextField16.setText(arr1.get(4));
        jTextField17.setText(arr1.get(5));
        jTextField18.setText(arr1.get(6));
        jTextField19.setText(arr1.get(7));
        jTextField20.setText(arr1.get(8));
        jTextField21.setText(arr1.get(9));
}