简体   繁体   中英

Java Servlet Static DAO

I've have tried to read about using static or not using static in my web application and wanted to just quickly ask if my implementation is good.

The following is my servlet

Integer total = HousingDAO.getTotal(AppUtils.getId(request));
Integer used = HousingDAO.getUsed(AppUtils.getId(request));

request.setAttribute("total", total);
request.setAttribute("used", used);
request.getRequestDispatcher("system/housing.jsp").forward(request, response);

And this is my DAO

public class HousingDAO {

public static Integer getTotal(String id){
    String sql_total = "SELECT count(*) FROM housing " + 
            "WHERE id = :id ";
    try (Connection con = ConnectionManager.getSql2o().open()) {
        return con.createQuery(sql_total).addParameter("id", id).executeScalar(Integer.class);
    }
}

public static Integer getUsed(String id){
    String sql_total = "SELECT count(*) FROM housing " + 
            "WHERE id = :id AND person IS NOT NULL";
    try (Connection con = ConnectionManager.getSql2o().open()) {
        return con.createQuery(sql_total).addParameter("id", id).executeScalar(Integer.class);
    }
}
}

So these are static, does it need to not be static, like this?

HousingDAO dao = new HousingDAO();
Integer total = dao.getTotal(AppUtils.getId(request));
Integer used = dao.getUsed(AppUtils.getId(request));

request.setAttribute("total", total);
request.setAttribute("used", used);
request.getRequestDispatcher("system/housing.jsp").forward(request, response);

With this DAO

public class HousingDAO {

public Integer getTotal(String id){
    String sql_total = "SELECT count(*) FROM housing " + 
            "WHERE id = :id ";
    try (Connection con = ConnectionManager.getSql2o().open()) {
        return con.createQuery(sql_total).addParameter("id", id).executeScalar(Integer.class);
    }
}

public Integer getUsed(String id){
    String sql_total = "SELECT count(*) FROM housing " + 
            "WHERE id = :id AND person IS NOT NULL";
    try (Connection con = ConnectionManager.getSql2o().open()) {
        return con.createQuery(sql_total).addParameter("id", id).executeScalar(Integer.class);
    }
}
}

Just would like to know if the first is ok, or do I need to do it like the second one?

EDIT

This is the ConnectionManager class

public static Sql2o getSql2o(){
    try {
        Class.forName(driver);
    } catch (ClassNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    try {
        return new Sql2o(PropertiesManager.getProperty("dburl")
                + PropertiesManager.getProperty("dbname"),
        PropertiesManager.getProperty("dbusername"),
        PropertiesManager.getProperty("dbpassword"));
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return null;

}

There are rarely any good reasons for using static methods. It is most often used in utility classes and classes that in their nature are singletons, like javas System class.

In your case, having a DAO class with static methods is according to me a bad idea. If the methods are static anything they reference has to be static. What if you want to reuse your DAO class to connect to several different databases?

A better approach is to inject all your dependencies in your DAO class, like the static ConnectionManager, and let the application decide how instances are wired together, not the classes themselves.

So the short answer is, use the second solution, but also remove the static access to ConnectionManager.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM