简体   繁体   中英

Java/sql experimental DAO implementation

I am working on a DAO implementation of a database in a Java (web) application. Only I have run into a slight issue.

My current code:

Account.java:

package beans;

public class Account {
    private int accountId;
    private String name;
    private String password;
    private String email;

    public Account() {

    }

    public Account(final String name, final String password, final String email) {
        this.name = name;
        this.password = password;
        this.email = email;
    }

    public int getAccountId() {
        return accountId;
    }

    public void setAccountId(int accountId) {
        this.accountId = accountId;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }
}

DAO.java:

package dao;

import java.util.Collection;

public interface DAO<T> {
    public int insert(T t);

    public boolean update(T t);

    public T get();

    public Collection<T> search();

    public boolean delete(T t);
}

AccountDAO.java:

package dao;

import beans.Account;
import java.util.Collection;

public class AccountDAO implements DAO<Account> { 
    @Override
    public int insert(Account t) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public boolean update(Account t) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public Account get() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public Collection<Account> search() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public boolean delete(Account t) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}

The thing is that I am pretty sure that I do not want to use SQL strings already in the DAO interface, but then I have the issue of how will I implement get() and search() properly?

As it is a generic interface, I cannot work with the columns names yet and I would like to keep the get() and search() methods inside the interface, to ensure that they will be implemented, unless it is really neccessary to remove them from the interface.

My proposed suggestion is to search by a range or by an not-completely filled in object of class T. So if you would want to get the account with name=x, then you would have code like:

AccountDAO accountDAO = DAOFactory.getAccountDAO();
Account result = accountDAO.get(new Account(x, null, null));

But I am unsure if this would be the best solution, please help me.

Regards.

This is how I tackled mine:

I created an abstract class NativeQueryBasedDAO that accepts SQL queries. This way, I can have subclasses that extends NativeQueryBasedDAO that represents eeach RDBMS (like MySQL, DB2, PostGres, etc.).

In your case, you will have something of this effect:

public abstract class NativeQueryBasedDAO<T> implements DAO<T> {

    private Connection connection;
    private String schemaName;
    private String tableName;

    protected NativeQueryBasedDAO(Connection connection, String tableName) {
        this(connection, null, tableName);
    }

    protected NativeQueryBasedDAO(Connection connection, String schemaName, String tableName) {
        if (schemaName == null) {
            this.schemaName = "";
        } else {
            this.schemaName = schemaName;
        }
        this.connection = connection;
        this.tableName = tableName;
    }

    protected final String getTableName() {
        return tableName;
    }

    protected final String getSchemaName() {
        return schemaName;
    }

    protected final Connection getConnection() {
        return connection;
    }
}

The above fields can help you in methods such as get() where you need to get an Object based on your schemaName + tableName .

Then, I would have an Factory<T> that has a public T create() that creates an object (in your case, account).

So, based on some Parameters, you can pass/generate a Factory` that will generate an object.

You can then modify your DAO to do a get(Criteria criteria) that builds a Factory with parameters to populate to create your object.

In a nutshell, it is not a simple solution (as this must be robust and grow based on your requirement). ORM such as Hibernate or JPA implementations (eg TopLink) handles this.

I hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM