9 Replies Latest reply: Jan 18, 2008 3:18 AM by 807603 RSS

    database insertion

    807603
      Hi

      i want to insert total of suppose 20 nos into database out of which 1st 10 for 1st parameter and next 10 for next parameter..
      then how will be the for loop..

      plz reply..

      ok.
      byee..
        • 1. Re: database insertion
          masijade
          If I understood you
          array[i]
          //and
          array[i+10]
          A better description and the posting of your current code would help greatly, however. And be forewarned, we are not simply going to "give you teh codez". We will point you in the right direction, but you're going to have to write it.
          • 2. Re: database insertion
            807603
            sry for double posting..


            but i m not getting ...
            i m sending what i have..
            int[] id
            int datalength (which is legth of each id)
            int[] data..


            for(int j=0 ; j<Id.length ; j++)
            {
            pstmt.setInt(1, paraDetailsId[j]);
            for(int i=1 ; i<=datalength[j] ;i++)
            {
                 pstmt2.setDouble(2, data);
                 pstmt2.executeUpdate();
            }
            }


            then how it will be..

            plz reply..
            • 3. Re: database insertion
              masijade
              Use Code Tags.

              Post your code as follows
              &#91;code&#93;
              // your code here
              &#91;/code&#93;


              And provide the query string and provide an example of what is to be added.

              i.e.

              is this what you want
              id[] = { 1, 2, 3, 4 }
              data[] = { a, b, c, d }
              
              // do you want produce the following combinations
              // 1 a
              // 1 b
              // 1 c
              // 1 d
              // 2 a
              // 2 b
              // 2 c
              // 2 d
              // 3 a
              // 3 b
              // 3 c
              // 3 d
              // 4 a
              // 4 b
              // 4 c
              // 4 d
              
              // or do you want produce the following combinations
              // 1 a
              // 2 b
              // 3 c
              // 4 d
              or something completely different.
              • 4. Re: database insertion
                807603
                i want..

                1 {a,b}
                2 {c,d}

                or

                for eg--

                id[] = {1,2}
                data={1,2,3,4,5,6,7,8,9,10}

                for id 1-- data 1to 5
                for id 2- data 6 to 10

                Edited by: abhi_blog on Jan 17, 2008 5:07 AM
                • 5. Re: database insertion
                  masijade
                  Well, I would say, you are then much better off storing your data in a hashmap. The first array should be the keys, and the values of the hashmap will be an array of values that correspond to that key.

                  If you cannot do this, and the difference is always 5 to 1, or whatever ratio as long as it is always the same, then you can do as follows:
                  int x = 5;  // or whatever the ratio is
                  for (int i = 0; i < id.length; i++) {
                    ps.setInt(1, id);
                  for (int j = i * x; j < (i * x + x); j++) {
                  ps.setString(((j % x) + 2), data[j]);
                  }
                  ps.execute();
                  }



                  I do not especially reccommend this approach, however. I would use the hashmap approach.
                  • 6. Re: database insertion
                    807603
                    ok

                    thanks for reply..

                    it looks some waht complicated..

                    do u have any idea about 2D arrays??


                    ok

                    byee
                    • 7. Re: database insertion
                      masijade
                      Like I said, hashmap.

                      If the data is that closely related, they should be contained in the same structure anyway. Saving them in multiple structures too easily allows for a "mix-up".
                      • 8. Re: database insertion
                        807603
                        I dont have any about Hashmap

                        can u give me any simple example which demonstrate the idea of hashmap..

                        i'll also look at other sources for hashmap..

                        thanks..
                        byee
                        • 9. Re: database insertion
                          807603
                          http://java.sun.com/docs/books/tutorial/collections/interfaces/map.html
                          http://java.sun.com/j2se/1.5.0/docs/api/java/util/HashMap.html