-2

I have 3 array's with different length. I need to insert the values present in all 3 array's into database table

This my array :

//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

I will retrieve highest length among the 3 array's

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

and place the highest length as base value in for loop

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]);
}

but I get error java.lang.ArrayIndexOutOfBoundsException

I need result as shown below

-----------------------------------------------
|   a        |       b       |      c         |
-----------------------------------------------
|  23        |  500000       |  3             |
-----------------------------------------------
|  4         |  500000       |  4             |
-----------------------------------------------
|  10        |  500000       |  6             |
-----------------------------------------------
|            |  300000       |  8             |
-----------------------------------------------
|            |  600000       |                |
-----------------------------------------------
Bugs
  • 4,491
  • 9
  • 32
  • 41
biladina
  • 137
  • 1
  • 11
  • Think about your database design – Jens Jun 28 '17 at 11:29
  • Try NoSQL - MongoDB maybe? – xenteros Jun 28 '17 at 11:30
  • Try to use a "while" structure instead of "for". – alex.pulver Jun 28 '17 at 11:32
  • @biladina assign null if you dont have any value in an object so that you can have same length for all bean classes. you can also insert the same null into database. you don't need to change any database design – divine Jun 28 '17 at 11:36
  • @divine the problem is I cannot insert data into database because of different element length in my array and the array element is dynamic from input field.. – biladina Jun 28 '17 at 11:40
  • @biladina then you can try inserting value as json into database. there are certain database that offers json datatype. if it is not available you can insert json as a string – divine Jun 28 '17 at 11:48
  • @biladina you get ArrayIndexOutOfBoundsException because some of your array length is less than the maximum length. this is a java exception. this exception is not related to database – divine Jun 28 '17 at 11:51
  • Please don't append _solved_ to the title. By accepting an answer you are already marking this question as solved. – Bugs Jun 28 '17 at 14:48

2 Answers2

1

You fetch value out of range in model.getId_hutang() and model.getId_keagenan().

try This edited

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);
} 

Now result look like -

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

find more about ArrayIndexOutOfBoundsException

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);
}