0

2 つのテーブルとそれらの間に 1 つの分解があります。

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 に答える