有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如果combobox的值保持为空,则必须将其插入MS Access

我在数据库中有4列,我必须在这些列中插入combobox的项。我尝试了下面的代码,但它只在combobox中插入4项时起作用。若我只在combobox中插入了两个项目,那个么剩余的两个项目必须插入NULL

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(0)+"',
                '"+cmb_route.getItem(1)+"',
                '"+cmb_route.getItem(2)+"',
                '"+cmb_route.getItem(3)+"')";

     s1.execute(q1);


     }

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

当我插入两个项目并且两个项目保持空白时,我得到以下错误:

java.lang.ArrayIndexOutOfBoundsException: 3 >= 2
java.lang.StackTraceElement;@1d8957f


共 (0) 个答案