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 ?