Stevenyc091
Stevenyc091

Reputation: 315

Compilation Error in Bean Class using DAO

I have an xhtml file that is outputting text from a bean class. The program connects to a MySql database using DAO architecture. My code below is trying to query the database to display a list of all entries. I am receiving the below error in MyFixBean class.

My xhtml file:

<h:outputText value="#{myFixBean.mydata}" /></h:column>

My java files (simplified for spacing):

public class MyFixBean {    

private DAOFactory fix = DAOFactory.getInstance("fix_site2.jdbc");
private MyDataDAO datalist = fix.getMyDataDAO();
private List<MyData> mydata = datalist.list();

public List<MyData> getMydata() {
    return mydata;
  }     
}

public abstract class DAOFactory {
    public MyDataDAO getMyDataDAO() {
        return new MyDataDAOJDBC(this);
    }
}

public interface MyDataDAO {
    List<MyData> list() throws DAOException;
}

public class MyDataDAOJDBC implements MyDataDAO {
   private DAOFactory daoFactory;
   private static final String SQL_FIND_ALL = "SELECT * FROM account_list";

   MyDataDAOJDBC(DAOFactory daoFactory) {
        this.daoFactory = daoFactory;
    }
   public List<MyData> list() throws DAOException {
    Connection connection = null;
    PreparedStatement preparedStatement = null;
    ResultSet resultSet = null;
    List<MyData> data = new ArrayList<MyData>();

    try {
        connection = daoFactory.getConnection();
        preparedStatement = connection.prepareStatement(SQL_FIND_ALL);
        resultSet = preparedStatement.executeQuery();
        while (resultSet.next()) {
            data.add(map(resultSet));
        }
    } catch (SQLException e) {
        throw new DAOException(e);
    } finally {
        close(connection, preparedStatement, resultSet);
    }
    return data;
 }  
}

The error I receive is:

WARNING: StandardWrapperValve[Faces Servlet]: Servlet.service() for servlet Faces Servlet     threw exception
java.lang.Error: Unresolved compilation problem: 
    Unhandled exception type DAOException
    at mybeans.MyFixBean.<init>(MyFixBean.java:21)

where line 21:

private List<MyData> mydata = datalist.list();

Credit to BalusC. My program is based off his code from his DAO tutorial. I left the site out of my code to save space.

Upvotes: 0

Views: 190

Answers (2)

Luiggi Mendoza
Luiggi Mendoza

Reputation: 85779

This is not a JSF nor a Dao design pattern implementation issue. It's a basic Java problem: Unresolved compilation problem. Make sure you have a DAOException class inside package mybeans (or the package this exception should be).

Basic sample:

package mybeans;

public class DAOException extends RuntimeException {
    //implement the exception as you want/need...
}

Upvotes: 1

Lukas Eichler
Lukas Eichler

Reputation: 5903

private List<MyData> mydata = datalist.list();

You need to handle the possible DAOException when your datalist.list() throws one. Either put a try/catch around it or rethrow the exception

Upvotes: 0

Related Questions