user10479159
user10479159

Reputation:

Generic Interface in different classes

In my project, I have to use the database pretty often and I decided to create an interface and then implement it in different classes since they will all use the same methods.

public interface Project<E> {

        void createTable();

        void insert(E obj);

        E select(int id);

        void delete(int id);

        void update(E obj, int id);

}

I try to implement it in one of my classes like below:

public class Person implements Project {

//createTable method
//select method
//delete method

public void insert(Person p) {
        Connection connection = null;
        PreparedStatement ppStm = null;

        try {
            connection = ConnectionConfiguration.getConnection();
            ppStm = connection.prepareStatement("INSERT INTO person (first_name, last_name)"
                    + "VALUES (?,?,?)");
            ppStm.setString(1, p.getName());
            ppStm.setString(2, p.getLname());
            ppStm.executeUpdate();

        } catch(Exception e) {
            e.printStackTrace();
        } finally {

            if (ppStm != null){
                try {
                    ppStm.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                    }
            if (connection != null) {
                    try {
                        connection.close();
                    } catch (SQLException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }
            }
        }

    }

//update method similar to insert();

}

The problem is that when I try to override the insert() and update() methods it shows me an error message "the method must override or implement a supertype method". I used generics since I thought it would work in my code seeing as I have different classes of different objects to implement it to but I am not understanding the right implementation I guess. I would like to know how I can change my interface or its implementation in my classes so it can work. Also when I remove the @Override it removes the error but still shows that I am not implementing all the methods. Thank you in advance.

Upvotes: 1

Views: 63

Answers (1)

Pankaj
Pankaj

Reputation: 2708

You can use DAO pattern

public interface DAO<T> {

  void createTable();

  void insert(T t);

  T select(int id);

  void delete(int id);

  void update(T t, int id);
}

Implementation

class PersonDAO implements DAO<Person> {

  @Override
  public void createTable() {}

  @Override
  public void insert(Person person) {
    /// Connection connection = null;
    /// PreparedStatement ppStm = null;
    // Detailed implementation
    //// .............
  }

  @Override
  public Person select(int id) {
    return null;
  }

  @Override
  public void delete(int id) {}

  @Override
  public void update(Person person, int id) {}
}

Upvotes: 2

Related Questions