Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8792035
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:55:28+00:00 2026-06-13T22:55:28+00:00

i have a method which has to insert into two tables, i have used

  • 0

i have a method which has to insert into two tables, i have used batch insert for that.
my problem is it takes the second query and inserts in to that table and fails to insert into first table that is fails to take first query.

this is what is my code :

public String saveVillageDetails(VillagesViewModel viewmodel, String functionType) {
        int[] saveOrupdateStatus = null;
        StringBuffer disctrictQuery = new StringBuffer();
        String saveOrupdateStatusMessage = "";
        Connection connection = getConnection();
        PreparedStatement districtPS = null;
        ResultSet rs = null;
        if (connection != null) {

            try {
                 connection.setAutoCommit(false);
                 if(functionType.equals("add")){
                    // to insert in villages table
                        disctrictQuery.append(" INSERT INTO m_villages(districtid,village,creation_date,last_created_by) ");
                        disctrictQuery.append(" VALUES (?, ?, ?,?) ");
                        districtPS = connection.prepareStatement(disctrictQuery.toString());
                        districtPS.setInt(1, viewmodel.getDistrictid());
                        districtPS.setString(2, viewmodel.getVillage());
                        districtPS.setTimestamp(3, getCurrentDate());
                        districtPS.setInt(4,viewmodel.getUserid());
                        districtPS.addBatch();
                        // to insert in regions table
                        disctrictQuery=new StringBuffer();
                        disctrictQuery.append(" INSERT INTO m_regions(villageid,creation_date,last_created_by) ");
                        disctrictQuery.append(" VALUES (?, ?, ?) ");
                        districtPS = connection.prepareStatement(disctrictQuery.toString());
                        districtPS.setInt(1, getLatestVilalgeID());
                        districtPS.setTimestamp(2, getCurrentDate());
                        districtPS.setInt(3,viewmodel.getUserid());
                        districtPS.addBatch();

                    }else if(functionType.equals("edit")){
                        // to update villages table
                        disctrictQuery.append("UPDATE m_villages SET districtid=? ,village=? ,updation_date=? ,last_updated_by=? ");
                        disctrictQuery.append(" WHERE villageid=? ");
                        districtPS = connection.prepareStatement(disctrictQuery.toString());
                        districtPS.setInt(1, viewmodel.getDistrictid());
                        districtPS.setString(2, viewmodel.getVillage());
                        districtPS.setTimestamp(3, getCurrentDate());
                        districtPS.setInt(4,viewmodel.getUserid());
                        districtPS.setInt(5,viewmodel.getVillageid());
                        districtPS.addBatch();
                        disctrictQuery=new StringBuffer();
                        // to update regiions table
                        disctrictQuery.append("UPDATE m_regions SET villageid=?,updation_date=? ,last_updated_by=? ");
                        disctrictQuery.append(" WHERE regionid=? ");
                        districtPS = connection.prepareStatement(disctrictQuery.toString());
                        districtPS.setInt(1, viewmodel.getVillageid());
                        districtPS.setTimestamp(2, getCurrentDate());
                        districtPS.setInt(3,viewmodel.getUserid());
                        districtPS.setInt(4,getRegionID(viewmodel.getVillageid()));
                        districtPS.addBatch();

                    }
                saveOrupdateStatus = districtPS.executeBatch();

                if (saveOrupdateStatus.length > 0) {
                    if(functionType.equals("add")){
                         saveOrupdateStatusMessage = "Village Details Added successfully";
                    }else if(functionType.equals("edit")){
                        saveOrupdateStatusMessage = "Village Details Modified successfully";
                    }
                    connection.commit();
                    connection.setAutoCommit(true);
                } else {
                     if(functionType.equals("add")){
                         saveOrupdateStatusMessage = "Failed to Add Village Details";
                    }else if(functionType.equals("edit")){
                        saveOrupdateStatusMessage = "Failded to Modify Village Details";
                    }

                }
            } catch (Exception ex) {
                ex.printStackTrace();
                //use log to print the exception    ex.printStackTrace();
            } finally {
                try {
                closeConnection(connection, rs, districtPS);
            } catch (Exception ex) {
                ex.printStackTrace();
                //use logger here
            }
        }
    }
    return saveOrupdateStatusMessage;
}

when i printed the statement i get like this :

com.mysql.jdbc.ServerPreparedStatement[2] -  INSERT INTO m_regions(villageid,creation_date,last_created_by)  VALUES (18, '2012-11-06 17:41:25', 1) 

and the first query is missing in the batch.

what could be the problem.

Please help

Regards

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T22:55:29+00:00Added an answer on June 13, 2026 at 10:55 pm

    Please refactor your code in the way as below:

                if(functionType.equals("add")){
                    // to insert in villages table
                    districtPS = connection.prepareStatement("INSERT INTO m_villages(districtid,village,creation_date,last_created_by)  VALUES (?, ?, ?,?) ", 
                            Statement.RETURN_GENERATED_KEYS);
                    districtPS.setInt(1, viewmodel.getDistrictid());
                    districtPS.setString(2, viewmodel.getVillage());
                    districtPS.setTimestamp(3, getCurrentDate());
                    districtPS.setInt(4,viewmodel.getUserid());
                    districtPS.executeUpdate();
                    ResultSet keySet = districtPS.getGeneratedKeys();
                    int villageId = 0;
                    if (keySet.next()) {
                        villageId = keySet.getInt(1);
                    }
                    // to insert in regions table
                    districtPS = connection.prepareStatement(" INSERT INTO m_regions(villageid,creation_date,last_created_by)  VALUES (?, ?, ?) ", 
                            Statement.RETURN_GENERATED_KEYS);
                    districtPS.setInt(1, villageId);
                    districtPS.setTimestamp(2, getCurrentDate());
                    districtPS.setInt(3,viewmodel.getUserid());
                    districtPS.executeUpdate();
                }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a database that has two tables, one of which contains a foreign
I have a method which I am trying to call dynamically. That method has
Say I have a method that I wish to call which has an argument
I have a class that has an output() method which returns a matplotlib Figure
I have a process which has a notify method which receives as a parameter
I have a class with static method which has a local static variable. I
I have a seed object which has an instance method seed:fall() which is called
I have a winforms, and it connecting wit webservice. Webservice has method which create
I have a controller which has a method called history class UsersController < ApplicationController
Imagine you have class A which has code which runs as method M. And

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.