0

我有两张桌子和一张分解它们之间的东西。

Target_of_numbers
  - targetId (with auto increment)
  - name
Numbers
  - numberId (with auto increment)
  - msisdn

Target_of_numbers_Numbers
  - targetId
  - numberId

我想问一下,如果我使用这个 Java 类,如何插入这个关系

Connection conn = null;
PreparedStatement stmt = null;

有人可以给我一个建议吗?

4

1 回答 1

0

好的,这就是我的解决方案。我不知道它是否是最好的但有效:

这是主要方法:

public void importTargetValue(NumberImportData data, Target target, ImportType type) throws SQLException {      
    List<Statement> stmtList = new ArrayList<Statement>();

    Connection conn = null;
    PreparedStatement stmtTarget = null;
    PreparedStatement stmtNumber = null;
    PreparedStatement stmtTargetNumber = null;

    try {
        conn = getConnection();     
        Long returnedTargetId = insertTarget(conn, stmtTarget, target);

        for (TargetNumber t : data.getNumbers()) {
            Long returnedNumberId = insertNumber(conn, stmtNumber, t);
            //insert to decompozition
            insertTargetNumber(conn, stmtTargetNumber, returnedTargetId, returnedNumberId);
        }

    } finally {
        stmtList.add(stmtTarget);
        stmtList.add(stmtNumber);
        stmtList.add(stmtTargetNumber);
        closeDbResources(conn, stmtList);
    }               
}   

插入 Target_of_numbers:

private Long insertTarget(Connection conn, Statement stmtTarget, Target target) throws SQLException {
    PreparedStatement statement = (PreparedStatement) stmtTarget;
    ResultSet generatedKeys = null;

    try {
    statement = conn.prepareStatement("INSERT INTO stats_target (name) VALUES (?)", Statement.RETURN_GENERATED_KEYS);
    statement.setString(1, target.getName());

    int affectedRows = statement.executeUpdate();
    if (affectedRows == 0) {
        throw new SQLException("Creating target failed, no rows affected.");
    }

    generatedKeys = statement.getGeneratedKeys();
    if (generatedKeys.next()) {
        target.setTargetId(generatedKeys.getLong(1));
    } else {
        throw new SQLException("Creating target failed, no generated key obtained.");
    }      
    } finally {
        if (generatedKeys != null) try { generatedKeys.close(); } catch (SQLException logOrIgnore) {}
     }

    return target.getTargetId();
}

插入数字:

private Long insertNumber(Connection conn, PreparedStatement stmtNumber, TargetNumber t) throws SQLException {
    PreparedStatement statement = (PreparedStatement) stmtNumber;
    ResultSet generatedKeys = null;   

    try {
    statement = conn.prepareStatement("REPLACE INTO stats_number (msisdn) VALUES (?)", Statement.RETURN_GENERATED_KEYS);
    statement.setString(1, t.getMsisdn());

    int affectedRows = statement.executeUpdate();
    if (affectedRows == 0) {
        throw new SQLException("Creating number failed, no rows affected.");
    }

    generatedKeys = statement.getGeneratedKeys();
    if (generatedKeys.next()) {
        t.setNumberId(generatedKeys.getLong(1));
    } else {
        throw new SQLException("Creating number failed, no generated key obtained.");
    }      
    } finally {
        if (generatedKeys != null) try { generatedKeys.close(); } catch (SQLException logOrIgnore) {}
     }

    return t.getNumberId();     
}   

插入分解:

private void insertTargetNumber(Connection conn, PreparedStatement stmtTargetNumber, Long returnedTargetId, Long returnedNumberId) throws SQLException {
    PreparedStatement statement = (PreparedStatement) stmtTargetNumber;

    try {
        statement = conn.prepareStatement("INSERT INTO stats_target_number (targetId, numberId) VALUES (?, ?)");
        statement.setLong(1, returnedTargetId);
        statement.setLong(2, returnedNumberId);

        int affectedRows = statement.executeUpdate();
        if (affectedRows == 0) {
            throw new SQLException("Creating target failed, no rows affected.");
        } 
    } finally {

    }       
}

还有一些关闭语句的方法:

protected final void closeDbResources(Connection conn, List<Statement> stmt) {
    for (Statement s : stmt) {
        if (s != null) {
            log.debug("[SQL QUERY]: " + s);
            try {
                s.close();
            } catch (SQLException e) {}
        }           
    }
}
于 2013-08-21T11:15:37.043 回答