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

我想这样做我想将一个组合框的每个项目添加到 msaccess 数据库中,例如我想
这样做我的组合有 4 个字段一、二、三、四,而数据库有四个字段 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

4

0 回答 0