PreparedStatement is not working

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dmjpro
    Top Contributor
    • Jan 2007
    • 2476

    #16
    Originally posted by JosAH
    Sigh, did you move that executeBatch() call below the loop?

    kind regards,

    Jos

    No,
    Code:
    for(int i=0; i<vector.size(); i++){       
    pstmt.setString(3, (String)vector.elementAt(i));
    pstmt.addBatch();
    counter=pstmt.executeBatch();
    System.out.println("Counter: " + counter); //this prints 1
    }

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #17
      Originally posted by dmjpro
      No,
      Then, why are you using the batch facility? You only stick one statement in it and execute it immediately.

      kind regards,

      Jos

      Comment

      • dmjpro
        Top Contributor
        • Jan 2007
        • 2476

        #18
        Originally posted by JosAH
        Then, why are you using the batch facility? You only stick one statement in it and execute it immediately.

        kind regards,

        Jos
        lolz! I expected the answer exactly what you given :)
        Actually just i am just testing what happens. But my question is if i do like this then how does it work? I am not clearing batch then each time how it prints counter "one".

        Now i tested out this code.
        Code:
        for(int i=0; i<vector.size(); i++){       
                    pstmt.setString(3, (String)vector.elementAt(i));
                    pstmt.addBatch();
                }
                counter=pstmt.executeBatch();
                System.out.println("Counter: " + counter.length);
                for(int i=0; i<vector.size(); i++){       
                    pstmt.setString(3, (String)vector.elementAt(i));
                    pstmt.addBatch();
                }
                counter=pstmt.executeBatch();
                System.out.println("Counter: " + counter.length);
        I am getting counter 3 at each print. Then why clearBatch, where it comes in importance?

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #19
          Originally posted by dmjpro
          I am getting counter 3 at each print. Then why clearBatch, where it comes in importance?
          You are just printing the array length; also print the value of each element. The clearBatch() method doesn't do much: it clears the entire batch, that's all; I suspect that a batch will be cleared after an executeBatch() has completed.

          kind regards,

          Jos

          Comment

          • dmjpro
            Top Contributor
            • Jan 2007
            • 2476

            #20
            Yeah i also think ;) Actually that's what i already said. Just i needed to confirm from the experts ;)

            Comment

            • JosAH
              Recognized Expert MVP
              • Mar 2007
              • 11453

              #21
              Originally posted by dmjpro
              Yeah i also think ;) Actually that's what i already said. Just i needed to confirm from the experts ;)
              You didn't say anything; you were just obfuscating the entire thing by executing a batch at every loop pass (and ignoring the tip I gave about it in reply #6). You also just printed the length of the counter array which only reflects the number of statements in the batch. Next you added a uniqueness constraint on your table and were flabbergasted that nothing (non-unique) was added to your table.

              Please read the replies from the 'experts' before you start to experiment in the wild.

              kind regards,

              Jos

              Comment

              • dmjpro
                Top Contributor
                • Jan 2007
                • 2476

                #22
                Originally posted by dmjpro
                Does executeBatch clear the previous set of parameters ?
                What's this? Actually i wanted to know whether executeBatch does the job like clearBatch after query execution. Anyway if i did anything wrong then sorry! :(

                Comment

                Working...