创建抽象模型以检索数据

时间:2016-06-12 00:30:49

标签: java database abstract-class

我试图在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 个答案:

答案 0 :(得分: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; } }

这更清晰,可以解决您可能遇到的任何系统资源问题。