Java Questions
Java Questions

Reputation: 7953

batch insert takes only one query and fails to take all the query in java

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

Upvotes: 0

Views: 216

Answers (1)

jdevelop
jdevelop

Reputation: 12296

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();
            }

Upvotes: 1

Related Questions