一个组合框无法将所有项目添加到ms访问数据库中

时间:2013-03-04 12:07:14

标签: java ms-access-2010

>  [i want to add all combobox item in msaccess database]   

我想这样做 我想将一个组合框的每个项目添加到msaccess数据库中,例如我想要 要做到这一点我的组合有4个字段一,二,三,四和数据库有四个fiels route1,route2,route3,route4 ...我想设置为route1 = one,route2 = two ....等。 如果我在组合框中添加了2个项目,那么剩下的两个不会留空。

if(e.getSource()==btn_save)
{
    try{

         Connection con;

         DriverManager.registerDriver(new sun.jdbc.odbc.JdbcOdbcDriver());

         Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

         con = DriverManager.getConnection("jdbc:odbc:dsnproj","","");

         Statement s1 = con.createStatement();

         String q1="insert dest,route1,route2,route3,route4 into trans_dest values(
                    '"+cmb_dest.getSelectedItem()+"',
                    '"+cmb_route.getItem(1)+"',
                    '"+cmb_route.getItem(2)+"',
                    '"+cmb_route.getItem(3)+"',
                    '"+cmb_route.getItem(4)+"')";

         s1.execute(q1);


         }

         catch(Exception ae)
         {
             System.out.println(ae);
         }
    }
}

  public void focusLost(FocusEvent e) {
   // throw new UnsupportedOperationException("Not supported yet.");

    if(e.getSource()==txt_route)
    {
      // transport t=new transport();

       JFrame frame = new JFrame();
       String[] options = {"Yes", "No"};

       int n = JOptionPane.showOptionDialog(frame,
        "Do you want to add more routes?",
        "",
        JOptionPane.YES_NO_OPTION,
        JOptionPane.QUESTION_MESSAGE,
        null, //do not use a custom Icon
        options, //the titles of buttons
        null); // disabled the ENTER Key

 System.out.println(n);

 if (n == JOptionPane.YES_OPTION) {

     cmb_route.add(txt_route.getText());

     txt_route.setText("");

 } else {

     cmb_route.add(txt_route.getText());
     txt_route.setText("");

 }
}

int i=cmb_route.getItemCount();
        if(i==4)
        {
            s1=cmb_route.getItem(0);
            s2=cmb_route.getItem(1);
            s3=cmb_route.getItem(2);
            s4=cmb_route.getItem(3);

            cmb_route.insert(s1, 0);
            cmb_route.insert(s2, 1);
            cmb_route.insert(s3, 2);
            cmb_route.insert(s4, 3); 

        }
        else
        if(cmb_route.getItemCount()==3)
        {
           s1=cmb_route.getItem(0);
            s2=cmb_route.getItem(1);
            s3=cmb_route.getItem(2);
            cmb_route.insert(s1, 0);
            cmb_route.insert(s2, 1);
            cmb_route.insert(s3, 2);
            cmb_route.insert("", 3);
        }

        else
        if(cmb_route.getItemCount()==2)
        {


             s1=cmb_route.getItem(0);
            s2=cmb_route.getItem(1);
            cmb_route.insert(s1, 0);
            cmb_route.insert(s2, 1);
            cmb_route.insert("", 2);
            cmb_route.insert("", 3);
        }
        else
        if(cmb_route.getItemCount()==1)
        {

            s1=cmb_route.getItem(0);
            cmb_route.insert(s1, 0);
            cmb_route.insert("", 1);
            cmb_route.insert("", 2);
            cmb_route.insert("", 3);

        }


    }

[我收到错误]     java.lang.ArrayIndexOutOfBoundsException:3> = 2

0 个答案:

没有答案