2016-06-12 98 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