As a learn-by-doing project to learn Java webapps, I'm creating a website for registering and displaying happenings using JSP/Servlets, Apache Tomcat server, JDBC and a MySQL database.
I use JSP for views, Servlets for controller functionality and business logic, and I've created ONE DBManager.java class (my only DAO) to abstract SQL statements from my Servlets and JSPs. In my Servlets, I use the DBManager
class by declaring a new instance and calling its public methods.
For instance:
DBManager dbm = new DBManager();
Boolean userIsUpdated = dbm.updateUserInDB(user);
My DAO looks briefly like this:
public class DBManager {
final String DATABASE_URL = "NOPE, I won't give you this one ;)";
Connection connection = null;
PreparedStatement pstmt = null;
public DBManager() {
//Nothing in constructor yet.
}
public ArrayList<Happening> getAllActivatedHappenings() {
//code removed for readability
}
public User logInAndGetUserByEmailAndPassword(String email, String password) {
//code removed for readability
}
public User insertUserIntoDB(User user) {
//code removed for readability
}
public boolean updateUserInDB(User user) {
//code removed for readability
}
public boolean insertHappeningIntoDB(Happening happening) {
//code removed for readability
}
public boolean deleteHappeningFromDB(int happeningId, int userId) {
//code removed for readability
}
public ArrayList<HappeningType> getAllHappeningTypes() {
//code removed for readability
}
public ArrayList<Happening> getAllHappeningsByUserId(int userid) {
//code removed for readability
}
public byte[] getPhotoByHappeningId(int happeningId, String imageType) {
//code removed for readability
}
public Map<Integer, String> getCountiesMap() {
//code removed for readability
}
public Boolean userIsHappeningOwner(int userId, int happeningId) {
//code removed for readability
}
public Happening getHappeningByHappeningId(int happeningId) {
//code removed for readability
}
public boolean updateHappening(Happening happeningDummy, String updateImage) {
//code removed for readability
}
public ArrayList<Happening> getFilteredActivatedHappenings(HttpServletRequest request) {
//code removed for readability
}
public boolean activateHappening(int happeningId) {
//code removed for readability
}
public boolean updateEmailNotificationForUser(User user, String emailnotificationhappeningtypeids, String emailnotificationcountyids) {
//code removed for readability
}
public User getUserByEmail(String email) {
//code removed for readability
}
}
As you can see, my DAO handles quite a lot of different transfer objects (happenings, users, cities, happening types etc.). Should I break this DAO into several smaller DAO classes (like HappeningDAO
, UserDAO
, etc.)? Any other reasons for doing this than readability?
Also, is this approach thread-safe as long as I declare a new instance of DBManager
within my doGet
/doPost
methods in the servlets?