在一个函数中使用JDBC将数据插入到多个表中

时间:2017-06-08 21:46:42

标签: java spring postgresql jdbc

我需要在我的spring boot webapp中执行数据库插入。到目前为止我所拥有的是一系列体育比赛和各自的信息。

一场比赛可以在同一天的不同时间举行。 烦恼我的拳头是我如何存储这个时间是DB。这里需要新表吗? (我创建了一个日期表

我四处搜寻,我仍然不知道如何结合插入 竞赛信息及其日期在我的插入功能同时。

我的插入功能需要一些工作,我需要somme帮助。

answer很好,但它没有满足我的要求

我的数据库架构:

CREATE TABLE competition ( 
  competition_id integer PRIMARY KEY,
  nom varchar(128) NOT NULL,
); 


CREATE TABLE date ( 
  id integer PRIMARY KEY,
  date_time timestamptz,
  competition_id integer REFERENCES competition (competition_id)
);

Json数据:

{
    "id": "420",
    "name": "SOCCER",
    "dates": [
        "2016-05-12T03:00:00.000Z"
        "2016-05-12T04:00:00.000Z"
        "2016-05-12T05:00:00.000Z"
    ]
},
{
    "id": "220",
    "name": "BASKETBALL",
    "dates": [
        "2016-05-12T03:00:00.000Z"
        "2016-05-12T04:00:00.000Z"
    ]
}

我的竞赛班级:

public class Competition{
    private int id;
    private String name;
    private String[] dates;
    // setters ... getters
}

插入数据的功能:

private static final String INSERT_STMT =
      " insert into competition (id, name)"
    + " values (?, ?)"
    ;  


public int insert(Competition competition) {
    return jdbcTemplate.update(conn -> {
      PreparedStatement ps = conn.prepareStatement(INSERT_STMT);
      ps.setInt(1, competition.getId());
      ps.setString(2, competition.getName());
      return ps;
    });


    // insert also in date table ???
  }

2 个答案:

答案 0 :(得分:2)

首先,如果您需要数据一致性,那么您应该使用事务包装insert语句。要将数据插入多个表,您应该执行多个insert语句,与使用sql一样。如果您需要返回更新的行数,您可以创建包装类,您可以在其中存储并返回它。

答案 1 :(得分:1)

首先,我会在日期表自动增量中创建ID,因此您不必为每个日期提供ID并使用此Base-Query:

private static final String INSERT_DATES = "INSERT INTO date (date_time, competition_id) VALUES ";

然后像这样建立声明:

public int insert(Competition competition) {
    // All local variables must be final because the lambdas will be executed at a undefined time
    final int id = competition.getId();
    final String name = competition.getName();

    final String[] dates = competition.getDates();
    final String dateValueStr = String.join(", ", Collections.nCopies(dates.length, "(?, ?)"));

    // Execute Updates
    int updatedRows1 = jdbcTemplate.update(conn -> {
        PreparedStatement ps = conn.prepareStatement(INSERT_STMT);
        ps.setInt(1, id);
        ps.setString(2, name);
        return ps;
    });

    if (updatedRows1 < 1)
    {
        // Something went wrong
        return -1;
    }

    int updatedRows2 = jdbcTemplate.update(conn -> {
        PreparedStatement ps = conn.prepareStatement(INSERT_DATES + dateValueStr);

        int idx = 1;
        for (String date : dates)
        {
            ps.setString(idx, date); // date_time
            idx++;
            ps.setInt(idx, competitionID); // competition_id
            idx++;
        }

        return ps;
    });

    if (updatedRows2 < 1)
    {
        // Something went wrong
        // Rollback the first insert, idk how its made
        return -1;
    }

    return updatedRows1 + updatedRows2;
}