java - 如何将不同长度的多个数组插入数据库

时间:2017-06-28 11:26:32

标签: java arrays spring-mvc

我有3个不同长度的数组。我需要将所有3个数组中的值插入到数据库表

这是我的阵列:

//Array1 length
int id_hutang = model.getId_hutang().length; //3
//Array2 length
int jlh_pengeluaran = model.getJlh_pengeluaran().length; //5
//Array3 length
int id_keagenan = model.getId_keagenan().length; //4

我将检索3个阵列中最长的

int largest = Math.max(id_hutang,Math.max(jlh_pengeluaran,id_keagenan));

并将最高长度作为基本值放入for循环

for(int i=0; i<largest; i++)
{
   String sql = "INSERT INTO surat_jalan (a, b, c) VALUES (?, ?, ?)";
   template.update(sql, model.getId_hutang()[i], model.getJlh_pengeluaran()[i], model.getId_keagenan()[i]);
}

但我收到错误java.lang.ArrayIndexOutOfBoundsException

我需要如下所示的结果

-----------------------------------------------
|   a        |       b       |      c         |
-----------------------------------------------
|  23        |  500000       |  3             |
-----------------------------------------------
|  4         |  500000       |  4             |
-----------------------------------------------
|  10        |  500000       |  6             |
-----------------------------------------------
|            |  300000       |  8             |
-----------------------------------------------
|            |  600000       |                |
-----------------------------------------------

2 个答案:

答案 0 :(得分:1)

您在model.getId_hutang()和model.getId_keagenan()中获取超出范围的值。

尝试此编辑

int id_hutang = model.getId_hutang().length;
int jlh_pengeluaran = model.getJlh_pengeluaran().length;
int id_keagenan = model.getId_keagenan().length;

int largest = Math.max(id_hutang,Math.max(jlh_pengeluaran,id_keagenan));

for(int i=0; i<largest; i++)
{
   String sql = "INSERT INTO surat_jalan (a, b, c) VALUES (?, ?, ?)";
   template.update(sql, 
                     (i<id_hutang)?model.getId_hutang()[i]:0, 

                     (i<jlh_pengeluaran)?model.getJlh_pengeluaran()[i]:0,

                     (i<id_keagenan)?model.getId_keagenan()[i]:0);
} 

现在结果看起来像 -

-----------------------------------------------
|   a        |       b       |      c         |
-----------------------------------------------
|  23        |  500000       |  3             |
-----------------------------------------------
|  4         |  500000       |  4             |
-----------------------------------------------
|  10        |  500000       |  6             |
-----------------------------------------------
|   0        |  300000       |  8             |
-----------------------------------------------
|   0        |  600000       |  0             |
-----------------------------------------------

了解更多信息 ArrayIndexOutOfBoundsException

答案 1 :(得分:0)

for(int i=0; i<largest; i++)
{
   String sql = "INSERT INTO surat_jalan (a, b, c) VALUES (?, ?, ?)";
   String a="",b="",c="";
   if(i>id_hutang ){
    a="";
   }else{
    a=model.getId_hutang()[i];
   }
    if(i>jlh_pengeluaran ){
    b="";
   }else{
    b=model.getJlh_pengeluaran()[i];
   }
    if(i>id_keagenan ){
    c="";
   }else{
    c=model.getId_keagenan()[i];
   }

   template.update(sql, a,b,c);
}