0

The name of this question is jsf_dao_interface
Hello and Thank you in advance for any assistance.


System info:

netbeans,glassfish,MySQL

The Purpose of this post is:

I am trying to learn to use an interface. I was wondering if someone was familiar with using and or had info on using DAO (data access object) classes.

The functionality of this code is:

The Model beans define the properties that must be saved into the database.
View beans extend the model beans with UI-specific code: actions, validates, etc.
JSF creates instances of the view beans as specified in the faces-config.xml file, but the persistence layer of the example application works with model beans.

Therefore, the application needs a utility method like ModelUtils.copy(), which copies the properties of the view beans instantiated by JSF into the model objects created by the persistence layer, and vice-versa.

The ModelUtils class also lets you get the model resources (such as configuration parameters, SQL statements, and error messages) from a resource bundle named ModelResources.

Finally, ModelUtils has a getLoggedInDAO() method that returns an instance of the LoginDAO interface that defines the methods for selecting, inserting, updating and deleting Subscriber objects from/into the relational database.
The data access methods are called from the action methods of the view beans.
Each action method is bound to a submit button in a JSP page
When the user clicks the button, the Web browser sends the form data to the server.
The JSF framework validates the form data
and returns the form to the user if there are any errors.
Otherwise, the valid form data is stored into the properties of the managed bean, and JSF calls the action method that is bound to the clicked button.

My question is:

Could someone supply some examples or links to examples of the use of this structure? I only have one example and it uses Oracle and that is matters even more confusing.

The errors related to this code are:


Code description: JDBCLoginDAO.java (the code is a starting structure with many errors. Future questions related will be presented. So I can understand how to correctly write my application specific code).

package jsfdb.model.dao;

import jsfdb.model.LoginInfo;

import jsfdb.model.ModelUtils;
import jsfdb.model.err.IncorrectPasswordException;
import jsfdb.model.err.LoginException;
import jsfdb.model.err.ProfileException;
import jsfdb.model.err.SubscribeException;
import jsfdb.model.err.UnknownSubscriberException;
import jsfdb.model.err.UnsubscribeException;

import javax.sql.DataSource;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class JDBCLoginDAO implements LoginInfoDAO {
    private DataSource dataSource;

    public JDBCLoginDAO() {
        try {
            Context initCtx = new InitialContext();
            Context envCtx = (Context) initCtx.lookup(
                ModelUtils.getResource("JavaCompEnv"));
            dataSource = (DataSource) envCtx.lookup(
                ModelUtils.getResource("DataSource"));
        } catch (NamingException x) {
            ModelUtils.log(x);
            throw new InternalError(x.getMessage());
        }
    }

    private void close(Connection conn, PreparedStatement ps)
            throws SQLException {
        if (ps != null)
            ps.close();
        if (conn != null)
            conn.close();
    }

    public LoginInfo select(LoginInfo loginInfo)
            throws LoginException,
            
            IncorrectPasswordException {
        Connection conn = null;
        PreparedStatement ps = null;
        try {
            conn = dataSource.getConnection();
            ps = conn.prepareStatement(
                ModelUtils.getResource("SelectStatement"));
            ps.setString(1, loginInfo.getUid());
            ResultSet rs = ps.executeQuery();
           
            String password = rs.getString(1);
            if (!loginInfo.getPassword().equals(password))
                throw new IncorrectPasswordException();
           
            
            loginInfo.setProfile(rs.getString(1));
           
            return loginInfo;
        } catch (SQLException x) {
            ModelUtils.log(x);
            throw new LoginException();
        } finally {
            try {
                close(conn, ps);
            } catch (SQLException x) {
                ModelUtils.log(x);
                throw new LoginException();
            }
        }
    }
/*
    public void insert(LoginInfo loginInfo)
            throws SubscribeException {
        Connection conn = null;
        PreparedStatement ps = null;
        try {
            conn = dataSource.getConnection();
            ps = conn.prepareStatement(
                ModelUtils.getResource("InsertStatement"));
            ps.setString(1, loginInfo.getEmail());
            ps.setString(2, loginInfo.getPassword());
            ps.setString(3, loginInfo.getName());
            ps.setBoolean(4, loginInfo.isManager());
            ps.setBoolean(5, loginInfo.isDeveloper());
            ps.setBoolean(6, loginInfo.isAdministrator());
            ps.setInt(7, loginInfo.getSubscriptionType());
            int rowCount = ps.executeUpdate();
            if (rowCount != 1)
                throw new SubscribeException();
        } catch (SQLException x) {
            ModelUtils.log(x);
            throw new SubscribeException();
        } finally {
            try {
                close(conn, ps);
            } catch (SQLException x) {
                ModelUtils.log(x);
                throw new SubscribeException();
            }
        }
    }

    public void update(LoginInfo loginInfo)
            throws ProfileException {
        Connection conn = null;
        PreparedStatement ps = null;
        try {
            conn = dataSource.getConnection();
            ps = conn.prepareStatement(
                ModelUtils.getResource("UpdateStatement"));
            ps.setString(1, loginInfo.getPassword());
            ps.setString(2, loginInfo.getName());
            ps.setBoolean(3, loginInfo.isManager());
            ps.setBoolean(4, loginInfo.isDeveloper());
            ps.setBoolean(5, loginInfo.isAdministrator());
            ps.setInt(6, loginInfo.getSubscriptionType());
            ps.setString(7, loginInfo.getEmail());
            int rowCount = ps.executeUpdate();
            if (rowCount != 1)
                throw new ProfileException();
        } catch (SQLException x) {
            ModelUtils.log(x);
            throw new ProfileException();
        } finally {
            try {
                close(conn, ps);
            } catch (SQLException x) {
                ModelUtils.log(x);
                throw new ProfileException();
            }
        }
    }

    public void delete(LoginInfo loginInfo)
            throws UnsubscribeException {
        Connection conn = null;
        PreparedStatement ps = null;
        try {
            conn = dataSource.getConnection();
            ps = conn.prepareStatement(
                ModelUtils.getResource("DeleteStatement"));
            ps.setString(1, loginInfo.getEmail());
            int rowCount = ps.executeUpdate();
            if (rowCount != 1)
                throw new UnsubscribeException();
        } catch (SQLException x) {
            ModelUtils.log(x);
            throw new UnsubscribeException();
        } finally {
            try {
                close(conn, ps);
            } catch (SQLException x) {
                ModelUtils.log(x);
                throw new UnsubscribeException();
            }
        }
    }
 */

}

Thanks again.
-ceyesumma

Note:
………………………………………………………………………………………………

Code description

2
Contributors
1
Reply
3
Views
7 Years
Discussion Span
Last Post by ~s.o.s~
0

The problem I see here is:
- You don't have a business tier; any business you implement in your beans would have to be replicated when you are asked to use a different view technology. Also, placing the business logic in view components creates tight coupling which is undesirable as the code base grows
- Your DAO layer seems to be doing validations and throwing business exceptions. Realistically, there shouldn't be any *logic* inside a DAO, the DAO classes are only concerned with *data access* and not validation etc.

Refer to the J2EE design patterns for more details.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.