2.3. Write code to implement the DAO Pattern - Source Code

DAO example:

package c2.s3;

/**
 * @author Mikalai Zaikin
 */
public class Customer implements java.io.Serializable {
    // member variables
    int customerID;
    String name;
    String streetAddress;
    String city;
    // ...
}
					


package c2.s3;

import java.util.Collection;

/**
 * @author Mikalai Zaikin
 */
public interface CustomerDAO {
    public int insertCustomer(Customer c);
    public boolean deleteCustomer(Customer c);
    public Customer findCustomer(int id);
    public boolean updateCustomer(Customer c);
    public Collection<Customer> selectCustomers(String query);
}

					


package c2.s3;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Collection;

/**
 * @author Mikalai Zaikin
 */
public class DerbyCustomerDAO implements CustomerDAO {

    Connection con;
    ResultSet rs;
    Statement stmt;

    public DerbyCustomerDAO() throws SQLException {
        con = getConnection();
    }

    private Connection getConnection() throws SQLException {
        return DriverManager.getConnection("jdbc:derby:sample");
    }


    @Override
    public int insertCustomer(Customer c) {
        // ...
        return 0;
    }

    @Override
    public boolean deleteCustomer(Customer c) {
        // ...
        return true;
    }

    @Override
    public Customer findCustomer(int id) {
        // ...
        return null;
    }

    @Override
    public boolean updateCustomer(Customer c) {
        // ...
        return true;
    }

    @Override
    public Collection<Customer> selectCustomers(String query) {
        // ...
        return null;
    }
}

					

Professional hosting         Free 'Oracle Certified Expert Web Services Developer 6' Guide     Free SCDJWS 5.0 Guide