Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: preparedStatement.executeBatch failed

  1. #1
    Junior Member
    Join Date
    Apr 2011
    Posts
    19
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default preparedStatement.executeBatch failed

    Hello,
    I'm having problem with this function and I think I'm doing something basically wrong.

    I want to insert some records(200+) to DB in bulk. I don't get any errors or exception in the process
    and yet no record actually inserted to DB.

    I don't think it's problem in security or commit, since when I do similar process not in batch, it's working
    and the records inserted into DB.

    Appreciate any help.

    The code:

    /*
    * should create the league in DB at the start of the season
    * 
    * UNDER DEVELOPMENT
    */
    public void createLeagus() 
    {
         String sqlSelect=	"SELECT team_num, place  " +
    				"FROM clubs_information " +
    				"WHERE current_league = 1 " +
    				"ORDER BY place";
     
         String sqlInsert=	"INSERT INTO seasons_tables " +
    				"(year, team_num, round, w_group, wins, losts, draw, positive, negative, points) VALUES " +
    				"("+Season.getYear()+", ?, 1, ?, 0, 0, 0, 0, 0, 0)";
     
         System.out.println(sqlInsert);
         try
         {
    	resultSet=statement.executeQuery(sqlSelect);
     
            PreparedStatement preparedStatement=connection.prepareStatement(sqlInsert);
     
           while(resultSet.next())
    	{
    		int place=resultSet.getInt("place");
    		int group=0;
    		int teamNum=resultSet.getInt("team_num");
     
                   if(place%10 == 1 || place%10 == 0)
    		    group=1;
    		if(place%10 == 2 || place%10 == 9)
    		    group=2;
    		if(place%10 == 3 || place%10 == 8)
    		    group=3;
    		if(place%10 == 4 || place%10 == 7)
    		    group=4;
    		if(place%10 == 5 || place%10 == 6)
    		    group=5;
     
                    preparedStatement.setInt(1, teamNum);
    		preparedStatement.setInt(2, group);
    		preparedStatement.addBatch();
     
                    System.out.println("INSERT NUM: " + teamNum + "TO GROUP : " + group );
    	}
     
            preparedStatement.executeBatch();
     
        }
        catch(SQLException e)
        {
    	System.out.println(e.getMessage());
        }
    }


  2. #2
    Junior Member
    Join Date
    Aug 2012
    Posts
    13
    Thanks
    0
    Thanked 7 Times in 4 Posts

    Default Re: preparedStatement.executeBatch failed

    Hmm, just a thought off the top of my head.. setAutoCommit(false);?

  3. #3
    Junior Member
    Join Date
    Apr 2011
    Posts
    19
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Re: preparedStatement.executeBatch failed

    Hi,
    thanks for reply indeed this was the problem.

    but why do have to set auto commit false in order to work with bulk?

  4. #4
    Junior Member
    Join Date
    Aug 2012
    Posts
    13
    Thanks
    0
    Thanked 7 Times in 4 Posts

    Default Re: preparedStatement.executeBatch failed

    Disabling autocommit allows an application to decide whether or not to commit the transaction in the event that an error occurs and some of the commands in a batch cannot be processed successfully.
    That explains it.

  5. The Following User Says Thank You to Object For This Useful Post:

    liron50 (August 11th, 2012)

  6. #5
    Junior Member
    Join Date
    Apr 2011
    Posts
    19
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Re: preparedStatement.executeBatch failed

    OK. thanks.

Similar Threads

  1. Hibernate - NullPointerException : PreparedStatement.java:1709
    By KevinWorkman in forum Web Frameworks
    Replies: 6
    Last Post: June 7th, 2012, 08:19 AM
  2. Replies: 3
    Last Post: December 27th, 2011, 07:55 AM
  3. Instantiation of bean failed
    By peejayuk in forum Member Introductions
    Replies: 1
    Last Post: August 15th, 2011, 02:35 PM
  4. This is Cool and interesting but i failed
    By Jhovarie in forum Java SE APIs
    Replies: 0
    Last Post: March 6th, 2011, 11:10 PM
  5. [SOLVED] loop , passed and failed
    By chronoz13 in forum Loops & Control Statements
    Replies: 5
    Last Post: August 25th, 2009, 07:00 AM