0

I have a DataDAO class that my Servlets use for CRUD operations on several object types. When I started out I only had a few things to do with this class so it seemed ok. But now the project is getting bigger and bigger and every feature I add I have to add a new method to this class so I now have a ton of static methods. It seems like I should refactor this some how but not sure how. Is there a design pattern I could use? Or can someone explain why I should or should not worry about it ? I am only doing this for learning purpose so please don't tell me use some easy framework, I want to work with java as granularly as possible. Here is a typical example:

public static ArrayList<Card> getCardsForUser(UserAccount user) {

    //TODO: get the username and password then get all flashcards linked to that user and return them in a list
    ArrayList<Card> cardsForUser = new ArrayList<>();

    try(Connection conn = DriverManager.getConnection(DBURL, un, pw)) {
        PreparedStatement pstm = conn.prepareStatement("Select * From flashcard where fk_user_id = ?");
        pstm.setString(1,user.getUserID());
        ResultSet usersCards = pstm.executeQuery();

        while(usersCards.next()){
            String cat = usersCards.getString("category");
            if(cat == null) {
                cat = "null";
            }
            Card card_new = new Card(usersCards.getString("card"),usersCards.getString("answer"),usersCards.getInt("cardid"),cat,usersCards.getInt("times_right"),usersCards.getInt("times_wrong"));
            cardsForUser.add(card_new);
        }
        System.out.println("Card For User size: "+cardsForUser.size());

        return cardsForUser;


    } catch(SQLException e) {
        //TODO: what happens now ?
        e.printStackTrace();
        return null;
    }

}

My app basically is a way to create 'flashcards' to study with.

I have tried to create an interface "DataDAO" then have a subclass for each different object implement that interface. BUT some of the operations don't perfectly align with the interface AND it seems like a lot of unnecessary work. Why is this a good or not a good approach ?

MadMax
  • 605
  • 1
  • 6
  • 19

2 Answers2

1

The best practice in such scenarios is to create DAO classes for every object type.

The negative aspect of putting everything inside a global class is complexity, testability and readability.

Common interfaces can be used on all DAO if they all contain common methods. For example, you have 10 object types (tables) and every DAO has Create Read Update Delete methods.

You can copy some ideas from spring boot framework where such DAOs are always one per object type and incoorporate similar into your solution:

https://docs.spring.io/spring-data/data-commons/docs/1.6.1.RELEASE/reference/html/repositories.html

ramden
  • 798
  • 1
  • 9
  • 25
1

Your class name DataDAO itself hints that there's something wrong in the design. Better to use separate DAO classes for different object types. E.g. CardDAO, UserAccountDAO etc.

Why have you made this method static? I don't see a reason. I think the method signature could be something like:

public List<Card> getCardsForUser(String userId)

Notice that I'm returning List instead of ArrayList.

Since you are a learner, I will mention below points as well:

Avoid using multiple variables with similar names in the same scope. You have usersCards and cardsForUser in your code. This can be confusing. For ResultSet, you could use a name like resultSet or rs.

Follow Java naming conventions. So, it's better to use cardNew than card_new.

Prasad Karunagoda
  • 2,048
  • 2
  • 12
  • 16