0

我有一个必须插入两个表的方法,我为此使用了批量插入。我的问题是它需要第二个查询并插入到该表中,但无法插入到第一个表中fails to take first query

这就是我的代码:

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

当我打印声明时,我得到这样的:

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

并且批处理中缺少第一个查询。

可能是什么问题呢。

请帮忙

问候

4

1 回答 1

1

请按照以下方式重构您的代码:

            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();
            }
于 2012-11-06T12:17:36.780 回答