简体   繁体   中英

how to store two single dimensional array values into jdbc java bean values and stored it into mysql?

here I get Student id get correct format but their status i got last value only my java bean code

String[] arr1=new String[1000]; //stuid
     if(aa!=null)
     {
         arr1=aa.split(",");
     }
      String[] arr = new String[1000]; //status
     if(ss!=null){
         arr = ss.split(",");
     }
             for(int j=0;j<arr1.length;j++)
             {
                    for(int i=0;i<arr.length;i++)
                     {
                        bb.setStuid(arr1[j]); 
                        bb.setStatus(arr[i]);
                        bb.setSid(sid);
                         bb.setCid(cid);
                         bb.setTtid(ttid);
                         bb.setDate(date);
                         bb.setDid(did);
                         bb.setHour(hour);
                     }
                    bb=ad.AddAttendance(bb);
         }


    return bb;

}

see my images

and my mysql inserted value is result page in mysql database (wrong value)

But, what I want exactly correct data show in front end

Hi hope this will work.

You should only use single for loop .

String[] arr1=new String[1000]; //stuid
     if(aa!=null)
     {
         arr1=aa.split(",");
     }
      String[] arr = new String[1000]; //status
     if(ss!=null){
         arr = ss.split(",");
     }
             for(int j=0;j<arr1.length;j++)
             {

                        bb.setStuid(arr1[j]); 
                        bb.setStatus(arr[j]);
                        bb.setSid(sid);
                         bb.setCid(cid);
                         bb.setTtid(ttid);
                         bb.setDate(date);
                         bb.setDid(did);
                         bb.setHour(hour);

                    bb=ad.AddAttendance(bb);
         }


    return bb;

}

You are always looping the entire arr array for each of the arr1 element. And that's why for each stuid, status is the last value (since the last value is stored inside bb.setStatus() when the inner loop completes for one studId).

You have to use single loop. And I think you will get your desired result.

String[] arr1=new String[1000]; //stuid
if(aa!=null)
     {
         arr1=aa.split(",");
 }
  String[] arr = new String[1000]; //status
 if(ss!=null){
     arr = ss.split(",");
 }
         for(int j=0;j<arr1.length;j++)
         {

                bb.setStuid(arr1[j]); 
                bb.setStatus(arr[j]);  // It will take status of j'th studId
                bb.setSid(sid);
                bb.setCid(cid);
                bb.setTtid(ttid);
                bb.setDate(date);
                bb.setDid(did);
                bb.setHour(hour);
           }
           bb=ad.AddAttendance(bb);
     }


    return bb;

}

Hope this will help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM