473,405 Members | 2,160 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

PreparedStatement is not working

Hi

I have to insert multiple rows into a Table. I am inserting rows
using addBatch and executeBatch.
Each row has 5 columns. Four columns of each row contain
same value except last column.
So I am using for loop to set the last column value.

I am using JDK1.5, DB2 and WAS6.1. My connection is OK and setAutoCommit sets as false.

Expand|Select|Wrap|Line Numbers
  1.  
  2. String insertStr="INSERT INTO DB.TBLNAME (COL1, COL2, COL3,COL4,COL5) VALUES(?,?,?,?,?)";
  3.  
  4. PreparedStatement pstmt = con.prepareStatement(insertStr);
  5. pstmt.setString(1, COL1);     
  6. pstmt.setString(2, COL2);
  7. pstmt.setString(3, COL3);                              
  8. pstmt.setString(4, COL4);                              
  9.  
  10. int counter[]=null;
  11. for(int i=0; i<vector.size(); i++){                  
  12.     pstmt.setString(5, (String)vector.elementAt(i));
  13.         pstmt.addBatch();
  14.  
  15.         counter=pstmt.executeBatch();                    
  16. }
  17.  
  18.  con.commit();
  19.  pstmt.close();              
  20.  
  21.  
I am not getting any error message or exception but Nothing is inserted in
the table. I am not sure why data is not inserted in the table.

I would be glad if anyone kindly help me to solve this issue.

Thank you
Apr 25 '09 #1
21 11001
dmjpro
2,476 2GB
@kokababu
You are checking it in another session.
Whenever you open a connection then a database session opens up, if the setAutoComit(false) is done then the modified data reflected to that session only where you did the operations; otherwise the modified data reflected across all database session. If you check in same session then you must get the modified data.
Apr 25 '09 #2
Hi,
Thank you
I set setAutoComit true and removed commit() from my code.
But still, no data is inserted into the table. I am very confused.
Apr 26 '09 #3
JosAH
11,448 Expert 8TB
What do you see when you print the 'counter' array obtained in line #15?

kind regards,

Jos
Apr 26 '09 #4
my counter value is 1.
Apr 26 '09 #5
JosAH
11,448 Expert 8TB
@kokababu
Move that executeBatch() call out of your loop, i.e. swap lines #15 and #16.

kind regards,

Jos
Apr 26 '09 #6
Thank you,

Now, I am very surprised that not all data is inserted into the table.
I wanted to insert 5 rows data, only last rows has been inserted into table.
Apr 27 '09 #7
dmjpro
2,476 2GB
First of all sorry, I couldn't see your "con.commit" statement ;)
I ran your code it's running successfully.

Here is my code ...
Expand|Select|Wrap|Line Numbers
  1. Vector vector = new Vector();
  2.         vector.add("1");
  3.         vector.add("2");
  4.         vector.add("3");
  5.  
  6.         Connection con = null;
  7.         try{
  8.         DriverManager.registerDriver(new org.postgresql.Driver());
  9.         con = DriverManager.getConnection("","","");
  10.         con.setAutoCommit(false);
  11.         String insertStr="INSERT INTO gadget.test_tab (col2, col3,col1) VALUES(?,?,?)";
  12.         PreparedStatement pstmt = con.prepareStatement(insertStr);
  13.         pstmt.setString(1, "Debasis");
  14.         pstmt.setString(2, "DMJPRO");
  15.  
  16.         int counter[]=null;
  17.         for(int i=0; i<vector.size(); i++){       
  18.             pstmt.setString(3, (String)vector.elementAt(i));
  19.             pstmt.addBatch();
  20.             counter=pstmt.executeBatch();
  21.         }
  22.         con.commit();
  23.         pstmt.close();
  24.         con.close();
  25.  
The code is running.

Now my Question, when prepareStatement gets called, then a new set of parameters added to the preparedStatement object. Another call results the addition of another new set of parameters to preparedStatement object and so on ...
Finally when i call executeBatch then it executes with all added parameters (added after the clearBatch).
If i am not wrong then inside loop, first call excuteUpdate gets 1 set of parameters, second call 2 set of parameters and so on ..but still i am having only 3 new rows insertion .. ;)
Does executeBatch clear the previous set of parameters ?
Apr 27 '09 #8
JosAH
11,448 Expert 8TB
Is there a unique key set for that table? If so, that would explain a lot. btw, Did you print out that 'counter' array at every loop pass?

kind regards,

Jos
Apr 27 '09 #9
dmjpro
2,476 2GB
Yeah I have the unique key set for the table, then what would be the explaination ;)
And i printed counter.length, and i got each result "one".
Apr 27 '09 #10
JosAH
11,448 Expert 8TB
@dmjpro
That makes sense as well: you execute the batch everytime your program runs through your loop. Everything could've been found in the API documentation. You are trying to add the same row multiple times (check what you want to add each time through your loop).

kind regards,

Jos
Apr 27 '09 #11
dmjpro
2,476 2GB
@JosAH
I removed the Primary Key, but it shown me same result every time it inserted one row.
Apr 28 '09 #12
JosAH
11,448 Expert 8TB
@dmjpro
Were three rows inserted in your database? Did you remove the executeBatch() call from your loop (and moved it after the end of the loop)? What is the content of the 'counter' array at each loop pass?

You have to dissect your problem to be able to find what your problem actually is.

kind regards,

Jos
Apr 28 '09 #13
dmjpro
2,476 2GB
@JosAH
See the code was same as it was, simply i removed the primary key.
And got the counter as 1 on each loop pass.
Apr 28 '09 #14
JosAH
11,448 Expert 8TB
@dmjpro
Sigh, did you move that executeBatch() call below the loop?

kind regards,

Jos
Apr 28 '09 #15
dmjpro
2,476 2GB
@JosAH

No,
Expand|Select|Wrap|Line Numbers
  1. for(int i=0; i<vector.size(); i++){       
  2. pstmt.setString(3, (String)vector.elementAt(i));
  3. pstmt.addBatch();
  4. counter=pstmt.executeBatch();
  5. System.out.println("Counter: " + counter); //this prints 1
  6. }
  7.  
Apr 28 '09 #16
JosAH
11,448 Expert 8TB
@dmjpro
Then, why are you using the batch facility? You only stick one statement in it and execute it immediately.

kind regards,

Jos
Apr 28 '09 #17
dmjpro
2,476 2GB
@JosAH
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.
Expand|Select|Wrap|Line Numbers
  1. for(int i=0; i<vector.size(); i++){       
  2.             pstmt.setString(3, (String)vector.elementAt(i));
  3.             pstmt.addBatch();
  4.         }
  5.         counter=pstmt.executeBatch();
  6.         System.out.println("Counter: " + counter.length);
  7.         for(int i=0; i<vector.size(); i++){       
  8.             pstmt.setString(3, (String)vector.elementAt(i));
  9.             pstmt.addBatch();
  10.         }
  11.         counter=pstmt.executeBatch();
  12.         System.out.println("Counter: " + counter.length);
  13.  
I am getting counter 3 at each print. Then why clearBatch, where it comes in importance?
Apr 28 '09 #18
JosAH
11,448 Expert 8TB
@dmjpro
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
Apr 28 '09 #19
dmjpro
2,476 2GB
Yeah i also think ;) Actually that's what i already said. Just i needed to confirm from the experts ;)
Apr 28 '09 #20
JosAH
11,448 Expert 8TB
@dmjpro
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
Apr 28 '09 #21
dmjpro
2,476 2GB
@dmjpro
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! :(
Apr 28 '09 #22

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Dani | last post by:
Hi everyone Description of the problem: Using a PreparedStatement to write down an integer (int) plus a timestamp for testing purposes. When read out again the integer looks very different. We...
5
by: Dov Moryusef | last post by:
Hi to all, I’ve a table with an idendity column : CREATE TABLE USRDB2.MYTABLE ( "ID" INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 0, INCREMENT BY 1, NO CACHE), "CODE"...
6
by: Markus | last post by:
In my Java-application I create an PreparedStatement like this: PreparedStatement v_stmt = dbcon.prepareStatement("SELECT * FROM mytable WHERE UPPER(userID) LIKE UPPER(?)"); v_stmt.setString(1,...
3
by: info | last post by:
Hallo zusammnen, ich möchte einen VARBINARY-Wert aus einer Tabelle auslesen, diesen in einer Datei speichern und später wieder in eine Tabelle einfügen. Den Wert hole ich mit einem Select und...
1
by: ptkumar | last post by:
Hi All, I am facing one problem, i fetch the record from database using preparedstatement. The resultset contains morethan 300000 records. it got OutOfMemoryError. Plz any body can help me.
0
by: eonblue20 | last post by:
Hello I\\\'m new to DB2 and I\\\'ve been trying to create the MERGE statement using PreparedStatements in Java as a requirement for a project I\\\'m working on, but I get the following message: ...
6
by: redashley40 | last post by:
This is my first attempt in SQL and PreparedStatement I have add the PreparedStatement and I'm not to sure if I'm doing it correctly. When I do a test run on Choose 1 ,or 2 I get this error. Error...
0
by: whim | last post by:
Hello everyone! I am having my own db2 function. Function return table. FUNCTION GETTAB_MY_INIT( recid INT) RETURNS TABLE In java program i do: PreparedStatement ps =null; String s =...
1
by: manjava | last post by:
Hello, i want to write this requete: in preparedstatement but signe error why can any tell what can write thanks
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.