0

I have to execute multiple insert queries using JDBC for which I am trying to execute batch statement. Everything works fine in my code but when i try to see values in the table, the table is empty. Here is the code :

SessionImpl sessionImpl = (SessionImpl) getSessionFactory().openSession(); Connection conn = (Connection) sessionImpl.connection(); Statement statement = (Statement) conn.createStatement(); for (String query : queries) { statement.addBatch(query); } statement.executeBatch(); statement.close(); conn.close(); 

And the

List<String> queries 

contains insert queries like:

insert into demo values (null,'Sharmzad','10006','http://demo.com','3 Results','some values','$44.00','10006P2','No Ratings','No Reviews','Egypt','Duration: 8 hours','tour','Day Cruises'); 

And the table structure is like:

create table demo ( ID INTEGER PRIMARY KEY AUTO_INCREMENT,supplierName varchar(200),supplierId varchar(200),supplierUrl varchar(200),totalActivities varchar(200),activityName varchar(200),activityPrice varchar(200),tourCode varchar(200),starRating varchar(200),totalReviews varchar(200),geography varchar(200),duration varchar(200),category varchar(200),subCategory varchar(200)); 

No exception is thrown anywhere but no value is inserted. Can someone explain?

6
  • Remove 'null' from insert query Commented Oct 17, 2016 at 9:45
  • If i remove null (which is for auto-increment id), i get an exception of "Not enough column values" Commented Oct 17, 2016 at 9:46
  • I can't see conn.commit() did you configure "autocommit" for your connection Commented Oct 17, 2016 at 9:52
  • Take a look at this, might help! Commented Oct 17, 2016 at 9:56
  • primary key had default not null constraint and I just wonder why your insert query don't throw error message like ERROR: null value in column "ID" violates not-null constraint. Commented Oct 17, 2016 at 10:04

3 Answers 3

5

Most JDBC drivers use autocommit, but some of them do not. If you don't know, you should use either .setAutoCommit(true) before the transaction or .commit() after it..

Sign up to request clarification or add additional context in comments.

Comments

1

Could be a transaction issue. Perhaps you're not committing your transaction? If so, then it is normal not to see anything in the database.

You can check if this is the case by running a client in READ_UNCOMMITTED transaction mode, right after .executeBatch(); (but before close()) and see if there are any rows.

Comments

1

You don't should assign a value to ID add supply all the others columns name

 insert into demo ( supplierName ,supplierId ,supplierUrl ,totalActivities ,activityName ,activityPrice ,tourCode ,starRating ,totalReviews ,geography ,duration ,category ,subCategory ) values ( 'Sharmzad' ,'10006' ,'http://demo.com' ,'3 Results' ,'some values' ,'$44.00' ,'10006P2' ,'No Ratings' ,'No Reviews' ,'Egypt' ,'Duration: 8 hours ','tour' ,'Day Cruises' ); 

and add commit to your code

5 Comments

is it necessary to add ( supplierName ,supplierId ,supplierUrl ,totalActivities ,activityName ,activityPrice ,tourCode ,starRating ,totalReviews ,geography ,duration ,category ,subCategory ) before mentioning values?
@roger_that If you don't assign all the columns .. you must inform the query which column match the related value.. and this is done by an explicit naming
Did that. Nothing happening. Do i need to commit the connection as well?
Yeah. I had to commit. Thanks.
@roger_that . .. i have update the asnwer ..with the tips for commit

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.