DAOs provide a clean interface to the database for the other classes.
Let us see this with an example. We will create a DAO class for the User class (from the previous post).
In your /src/org/confucius, create a class UserDAO.java, like this:
package org.confucius;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource;
public class UserDAO {
private static MysqlConnectionPoolDataSource dataSource = null;
// Static initialization block
static {
dataSource = new MysqlConnectionPoolDataSource();
dataSource.setUser("confucius");
dataSource.setPassword("changeit");
dataSource.setServerName("localhost");
dataSource.setPort(3306);
dataSource.setDatabaseName("confuciusDB");
}
public static void persist (User user) throws SQLException{
Connection conn = dataSource.getConnection();
String sqlQuery = "insert into users value(?, ?)";
PreparedStatement ps = conn.prepareStatement(sqlQuery);
ps.setString(1, user.getFirstName());
ps.setString(2, user.getLastName());
ps.execute();
ps.close();
conn.close();
}
public static List<User> getUsers () throws SQLException{
List<User> users = new ArrayList<User>();
Connection conn = dataSource.getConnection();
String sqlQuery = "select firstName,lastName from users";
PreparedStatement ps = conn.prepareStatement(sqlQuery);
ResultSet rs = ps.executeQuery();
while (rs.next()){
String firstName = rs.getString("firstName");
String lastName = rs.getString("lastName");
User user = new User();
user.setFirstName(firstName);
user.setLastName(lastName);
users.add(user);
}
rs.close();
ps.close();
conn.close();
return users;
}
}
There are two methods in this class - one for persisting a user to the database, other for getting a list of users from the database.
The static block initializes the datasource.
Now, update your TestPersistence.java class to use the UserDAO, like this:
package org.confucius;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.List;
public class TestPersistence {
public static void main(String[] args) {
try {
List<User> users = UserDAO.getUsers();
for (Iterator<User> iter = users.iterator(); iter.hasNext(); ){
User user = iter.next();
System.out.println("Got user: " + user.getFirstName() + " " + user.getLastName());
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
As you can see, there is no reference to JDBC related code in this class. The UserDAO class has allowed for a clean separation of concerns.
R-click on TestPersistence.java in Eclipse-Navigator view, and select Run As--> Java Application. You will see the users printed to the console.
You can add more methods in the DAO class - for deleting users, getting users which match certain attributes, updating user information, etc.
In addition to providing a clean separation of database interaction, DAOs also provide convenience in refactoring code if your database is changed, or you decide to use another database interaction solution, like hibernate.
No comments:
Post a Comment