-1

我正在尝试使用 JDBC 将 FK 关系从 MySQL 实现到 JAVA。我有一个Garaz对象列表,每个对象Garaz都有一个Auto(汽车)对象列表。我有非常复杂的数据。

我的 MySQl DB 没问题,我尝试这样做:

public static ArrayList <Garaz> selectRecords() throws SQLException {
    Connection dbConnection = null;
    Statement statement = null;

    String selectTableSQL = "SELECT Garaz.G_ID, Garaz.Nazwa, Garaz.Adres, Garaz.LiczbaMiejsc, Garaz.LiczbaPoziomow, " +
        "Garaz.Czynny, Auta.A_Id, Auta.Model, Auta.Kolor, Auta.IloscDrzwi, Auta.Rejestracja\n" +
        "FROM Garaz\n" +
        "LEFT JOIN Auta\n" +
        "ON Garaz.G_Id=Auta.G_Id\n" +
        "ORDER BY Garaz.G_Id; ";

    // ArrayList lista = new ArrayList <Garaz>();

    try {
        dbConnection = getDBConnection();
        statement = dbConnection.createStatement();

        System.out.println(selectTableSQL);

        // execute select SQL stetement
        ResultSet rs = statement.executeQuery(selectTableSQL);

        while (rs.next()) {
            int g_id = rs.getInt("G_ID");
            String nazwa = rs.getString("NAZWA");
            String adres = rs.getString("ADRES");
            int lmiejsc = rs.getInt("LICZBAMIEJSC");
            int lpoz = rs.getInt("LICZBAPOZIOMOW");
            boolean czynny = rs.getBoolean("CZYNNY");

            ArrayList lista2 = new ArrayList <Auto>();

            int a_id = rs.getInt("A_Id");
            String model = rs.getString("Model");
            String kolor = rs.getString("Kolor");
            int ildrzwi = rs.getInt("IloscDrzwi");
            String rejestracja = rs.getString("Rejestracja");

            Auto d = new Auto(a_id, model, kolor, ildrzwi, rejestracja);
            if (a_id !=0){
                lista2.add(d);
            }
            Garaz f = new Garaz(g_id, nazwa, lista2, adres, lmiejsc, lpoz, czynny);
            lista.add(f);

            //System.out.println("nazwa : " + nazwa);
            //System.out.println("adres : " + adres);
            // return lista;
        }

    } catch (SQLException e) {
        System.out.println(e.getMessage());
    } finally {
        if (statement != null) {
            statement.close();
        }
        if (dbConnection != null) {
            dbConnection.close();
        }
    }
    return lista;
}

我不明白如何读取ResultSet rsArrayList Garaz包含对象(Garaz)并且每个Garaz对象包含ArrayList Autors因此,通过从( )读取数据来创建 2 个列表(一个是另一个列表的一部分)时,我遇到了很大的问题ResultSet。我拥有数据库表中的Garaz所有Auto内容,但关系是混合的。喜欢Garaz1包含随机Auto(汽车)。

如何创建 2 个列表(一个是另一个列表的一部分)以保持关系AutoGaraz基于的一部分G_ID

4

2 回答 2

1

您的结果集将为每个 Garaz 和 Auto 提供一个结果(又名行),因为这就是 select 语句的作用。这样你就可以...

要么按原样解析结果集,然后手动创建您想要的每个 Garaz & Auto 记录,但您将不得不处理重复的 Garaz 数据。

或者

您可以使用 MyBatis 之类的框架来获取对象,或者。

或者

对 Garaz 列表执行 SELECT 语句,然后执行另一个 SELECT 语句以获取每个 Garaz 的 AUTO 列表。


Sudo code.....  


@Repository
public class StoreDbDAO 
{
    @Autowired  
    public void init(@Qualifier("dataSourceCDB") DataSource dataSource) {
        this.dataSource = dataSource;
        this.simpleJdbcTemplate = new SimpleJdbcTemplate(dataSource);
    }

    private static final String GET_USABLE_RECORDS = "SELECT d.ID, d.HARDWARE_ID " +
            " FROM DEVICE d " +
            " LEFT JOIN TABLEB o on o.X_ID = d.X_ID " +
            " WHERE " +
            " d.DEVC_HARDWARE_ID IS NOT NULL " +
            " AND o.CODE = ?  "";


    public List<Map<String, Object>> getStores(String cCode)
    {
        return simpleJdbcTemplate.queryForList(GET_USABLE_RECORDS, code);
    }
}

@Autowired StoreDbDAO storeDbDAO;

public void caller() { List> stores = storeDbDAO.getStores();

List<Stores> storeRecords = new ArrayList[stores.size()];
for (Map<String, Object> store: stores)
{

  final String storeId = (String) store.get("HARDWARE_ID");
  StoreRecord x = new StoreRecord(storeId)
  storeRecords.add(x);


  List<Map<String, Object>> devicesInTheStore = storeDbDAO.getDevicesForStore(storeId);
  // convert these into something useful.
  x.setDevicesInStore(convertToList(devicesInTheStore));
}

}

于 2013-11-15T11:07:08.010 回答
0

您需要遍历结果,检查您是否已经为该行创建了Garaz对象,G_ID然后使用该对象或创建一个新对象。这可以通过对字段进行排序来简化,并在更改时G_ID创建一个新Garaz对象。G_ID

当您评论说您不知道如何执行此操作时,这是一个完整的示例:

public List<Garaz> getAllGaraz() throws SQLException {
    List<Garaz> garazList = new ArrayList<Garaz>();
    try (
        Connection con = getDBConnection();
        Statement stmt = con.createStatement();
        ResultSet rs = stmt.executeQuery(
            "SELECT Garaz.G_ID, /* other garaz columns */ " +
                   "Auta.A_Id /*other auta columns */\n" +
            "FROM Garaz\n" +
            "LEFT JOIN Auta\n" +
            "ON Garaz.G_Id=Auta.G_Id\n" +
            "ORDER BY Garaz.G_Id");
    ) {
        Garaz currentGaraz = null;
        while (rs.next()) {
            int garazId = rs.getInt("G_ID");
            // Create Garaz only if it is different
            if (currentGaraz == null || currentGaraz.getId() != garazId) {
                // retrieve other columns
                currentGaraz = new Garaz(g_id /* + other garaz columns */);
                garazList.add(currentGaraz);
            }

            int a_id = rs.getInt("A_Id");
            // replacement of your condition of a_id != 0
            // 0 could be a valid value, check for null instead
            if (!rs.wasNull()) {
                // retrieve other columns
                Auto auta = new Auta(a_id /* + other auta columns */);
                // The list of Auta is part of the garaz
                currentGaraz.addAuta(auta);
            }
        }
        return garazList;
    }
}

public class Garaz {
    private final List<Auta> autaList = new ArrayList<Auta>();
    private final int id;

    public Garaz(int g_id /* + other fields*/) {
        id = g_id;
    }

    public int getId() {
        return id;
    }

    public void addAuta(Auta auta) {
        autaList.add(auta);
    }

    public List<Auta> getAutaList() {
        return new ArrayList<Auta>(autaList);
    }
}
于 2013-11-15T11:40:21.267 回答