2016-06-12 97 views
0

我想在MVC中也有一個數據庫建立一個應用程序。我有多個模型,我正在從模型類中投射數據。隨着應用程序進展的繼續,它似乎有很多重複。在我的模型中的每個函數中,我需要打開一個連接,語句和結果集構建查詢等,並使用result.next();語句獲取數據。創建一個抽象模型來檢索數據

爲了擺脫這些重複,我開始使用名爲AbstractModel的抽象類。我的目標是將所有模型擴展到這個類並傳遞查詢。對我來說困難的部分是我需要在result.next()循環中檢索數據的位置。

我以爲我可以將結果集傳回給我的模型,但後來我無法再關閉我的資源。

我的模型類之一。

public class BezoekerModel 
{ 

    public Gebruiker getGebruiker(String username, String password) 
    { 
     Gebruiker user = null; 

     PreparedStatement stat = null; 
     ResultSet result = null; 
     Connection conn = null; 

     try 
     { 
      conn = SimpleDataSourceV2.getConnection(); 
      String query = "SELECT * FROM gebruiker WHERE gebruikersnaam = ? AND wachtwoord = ?;"; 
      stat = conn.prepareStatement(query); 
      stat.setString(1, username); 
      stat.setString(2, password); 
      result = stat.executeQuery(); 

      while (result.next()) 
      { 
       String gebruikerstype = result.getString("gebruikerstype"); 
       String voornaam = result.getString("voornaam"); 
       String tussenvoegsel = result.getString("tussenvoegsel"); 
       String achternaam = result.getString("achternaam"); 
       int schoolcode = result.getInt("schoolcode"); 

       user = new Gebruiker(voornaam, tussenvoegsel, achternaam, gebruikerstype, schoolcode); 
      } 
     } 
     catch (SQLException ex) 
     { 
      ex.printStackTrace(); 
     } finally 
     { 
      try 
      { 
       result.close(); 
       stat.close(); 
       conn.close(); 
      } 
      catch (SQLException ex) 
      { 
       System.out.println("Error: " + ex.toString()); 
      } 
     } 
     return user; 
     } 
    } 

正如你所看到的,我把我檢索到的所有數據都轉換成了Gebruiker對象,後來我也回來了。

這是我現在正在構建的抽象模型。

public class AbstractModel 
{ 

public List<Object> getData(String query) { 
    List<Object> data = new ArrayList(); 


    Statement stat = null; 
    ResultSet result = null; 
    Connection conn = null; 

    try { 
     conn = SimpleDataSourceV2.getConnection(); 
     stat = conn.createStatement(); 
     result = stat.executeQuery(query); 

     while (result.next()) { 
      // ????? 
     } 
    } catch (SQLException ex) { 
     ex.printStackTrace(); 
    } finally { 
     try { 
      result.close(); 
      stat.close(); 
      conn.close(); 
     } catch (SQLException ex) { 
      System.out.println("Error: " + ex.toString()); 
     } 
    } 
    return data; 
} 

正如我前面說過的,我想將要在bezoekerModel中創建的查詢傳遞給AbstractModel。我不知道這是否可能。如果你們中的任何一位對此提供了意見,我們將不勝感激,所以我不必再浪費任何時間。

+1

爲什麼你不使用圖書館? Spring的JdbcTemplate? –

+0

您是不是指'公共抽象類AbstractModel'?和'公共類BezoekerModel擴展AbstractModel'? – Buildersrejected

+0

@Buildersrejected是的,但是我展示的那些你還沒有得到那麼多。這只是爲了展示我的目標是什麼。這兩個班級現在沒有連接。 – Necati

回答

0

如果AbstractModel是父類,並且BezoekerModel是AbstractModel的子類,則可以在處理查詢的父類「AbstractModel」中創建一個方法,並將結果傳遞迴子類。

public abstract class AbstractModel { 

    //current arraylist code 
    PreparedStatement stat; 
    ResultSet result; 
    Connection conn; 
    protected ResultSet processQuery(String query,String username, String password) { 

    try { 
      conn = SimpleDataSourceV2.getConnection(); 
      String _query = query; 
      stat = conn.prepareStatement(query); 
      stat.setString(1, username); 
      stat.setString(2, password); 
    return result = stat.executeQuery(); 
    } 
    } catch (SQLException ex) { 
     //..... 
} 
} 

這應該建立您的查詢。現在你的BezoekerModel會這樣說:

public class BezoekerModel { 
public Gebruiker getGebruiker(String username, String password) { 
Gebruiker user = null; 
ResultSet result = processQuery("SELECT * FROM gebruiker WHERE gebruikersnaam = ? AND wachtwoord = ?;",username,password); 
    while (result.next()) { 
    String gebruikerstype = result.getString("gebruikerstype"); 
    String voornaam = result.getString("voornaam"); 
    String tussenvoegsel = result.getString("tussenvoegsel"); 
    String achternaam = result.getString("achternaam"); 
    int schoolcode = result.getInt("schoolcode"); 
    user = new Gebruiker(voornaam, tussenvoegsel, achternaam, gebruikerstype, schoolcode); 
    } 
} catch (SQLException ex) { 
    ex.printStackTrace(); 
} finally { 
    if (result != null || stat != null || conn != null) { 
      try { 
      result.close(); 
      stat.close(); 
      conn.close(); 
      } 
} catch (SQLException ex) { 
     System.out.println("Error: " + ex.toString()); 
} 
     } //end of if 
} return user; } } 

這是更清潔,應該解決您可能有的任何系統資源問題。

+0

這是我以前的想法,但我不知道如何返回結果集等,但是當你返回結果集時,conn和preparedStatement會導致系統阻塞系統資源? – Necati

+0

我不這麼認爲,BezoekerModel擁有你所調用的所有方法,理論上你只是簡單地調用同一個類中的第二種方法。如果您擔心繫統資源,可以將連接,查詢和結果集作爲類變量而不是實例變量。事實上,我會用這種方法更新我的答案。 – Buildersrejected