2013-11-14 45 views
-1

我試圖使用JDBC將MySQL中的FK關係實現到JAVA中。我有一個Garaz對象的列表,每個Garaz都有一個Auto(汽車)對象的列表。我有很混雜的數據。JDBC MYSQL FK實現

我的MySQL數據庫是好的,我嘗試做這樣的:

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 rs的方式閱讀:ArrayList Garaz包含對象(Garaz),並且每個Garaz對象包含ArrayList Auto。所以我通過從rsResultSet)讀取數據來創建2個列表(一個是另一個列表的一部分)時遇到大問題。我從數據庫表中獲得了全部Garaz和全部Auto,但關係是混合的。像Garaz1包含隨機Auto(汽車)。

如何創建2個列表(一個是另一個的一部分)以保持關係AutoGaraz的一部分,基於G_ID

回答

0

您需要遍歷結果,請檢查您是否已經創建了Garaz對象行的G_ID,要麼使用或創建一個新的。這可以通過對G_ID字段進行排序來簡化,並在G_ID更改時創建新的Garaz對象。

當你評論說,你不知道如何做到這一點,這裏是一個完整的例子:

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); 
    } 
} 
+0

好的。這聽起來像一個計劃:) thnx幫助我,我會嘗試這個evning。 – user2982527

+0

有關創建AutoList的ArrayList的一些技巧,這是Garaz對象的一部分。我知道哪個汽車(汽車)根據G_id在Auta表中去到哪個Garaz – user2982527

+0

將'Garaz'對象的'List'部分做成這樣,並且簡單地添加你從ResultSet創建的汽車(或者類似的東西,解決方案取決於如何使用結果)。我不想粗魯,但我會說,能夠想到這些解決方案是您作爲開發人員需要的基本解決問題技能的一部分。 –

1

你的結果集將爲每個Garaz和Auto提供一個結果(又名行),因爲那就是select語句的作用。所以你可以...

要麼解析結果集,因爲它是,並手動創建每個加拉茲&你想要的自動記錄,但你將不得不處理重複的Garaz數據。

OR

您可以使用像MyBatis的一個框架來獲取得到的對象回來,或。

OR

執行SELECT語句Garaz的列表中,然後執行另一個SELECT語句得到自動的回到每個Garaz名單。


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)); 
} 

}

+0

好了,所以:1。你能告訴我如何創建2周的ArrayList(一個是部分另一個基於G_ID)從這個rs(ResaultSet)?我有這個大問題,我的數據是混合的 – user2982527

+0

2.我現在想保持簡單,我對fw 3不太瞭解。我試過但:默認情況下,每個Statement對象只能有一個ResultSet對象可以同時打開,所以它也不起作用 – user2982527

+0

添加了一個sudo示例。 –