StringBuilder()在最后一次迭代时追加Transaction id

时间:2013-01-06 11:58:15

标签: mysql transactions stringbuilder

我在Xls文件中下面的代码上传下面的方法调用,直到最后一行Xls sheet&我将值附加到StringBuilder。

     public boolean insertBatch(Connection con, int projectId, int venueId, String batchName, String examStartDate, String examStartTime, String examEndTime, String reportingStartTime, String reportingEndTime, int totalSeats) {
    String  this.query = new StringBuilder().append("insert into venue_batch ( batch_code  ,exam_venue_id  ,exam_date  ,exam_start_time  ,exam_end_time  ,reporting_start_time  ,reporting_end_time  ,total_seats,tranID) VALUES ('").append(batchName).append("' ,").append(venueId).append(" ,'").append(stDate).append("' ,'").append(examStartTime).append("'  ,'").append(examEndTime).append("' ,'").append(reportingStartTime).append("' ,'").append(reportingEndTime).append("' ,").append(totalSeats).append(" )").toString();
                try {
                    this.stmt = con.createStatement();
                    count = this.stmt.executeUpdate(this.query);
                    this.stmt.close();
                } catch (SQLException ex) {
                    System.out.println(new StringBuilder().append("Venue # insertBatch # SQLException").append(ex.toString()).toString());
                    onUploadFail(con, "Please try again!");
                }
    }

*I need to add Unique Transaction id(VenueTranId) to DB at the last during executeUpdate(this.query), in the above  Query  how to Proceed below is Transaction id code.Thanks All..

          Calendar calendar = Calendar.getInstance();
      Long lngTime = Long.valueOf(calendar.getTimeInMillis());
      String VenueTranId=lngTime.toString();

1 个答案:

答案 0 :(得分:0)

将StringBuilder存储在变量中,以便稍后可以附加任何所需内容。

public boolean insertBatch(Connection con, int projectId, int venueId, String    batchName, String examStartDate, String examStartTime, String examEndTime, String reportingStartTime, String reportingEndTime, int totalSeats) {
StringBuilder query = new StringBuilder();
query.append("insert ");
query.append("more blabla ");

try {
  this.stmt = con.createStatement();
  count = this.stmt.executeUpdate(this.query.toString());
  this.stmt.close();
} catch (SQLException ex) {
                query.append(" Failed: "+ex.getMessage());
} finally {
  String VenueTranId = computeId();
  query.append(VenueTranId);
}