使用DAO的Bean类中的编译错误

时间:2013-11-16 16:58:21

标签: java

我有一个从bean类输出文本的xhtml文件。该程序使用DAO体系结构连接到MySql数据库。我下面的代码试图查询数据库以显示所有条目的列表。我在MyFixBean类中收到以下错误。

我的xhtml文件:

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

我的java文件(间距简化):

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;
 }  
}

我收到的错误是:

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)

第21行:

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

归功于BalusC。我的程序基于他的DAO教程中的代码。我从我的代码中删除了网站以节省空间。

2 个答案:

答案 0 :(得分:1)

这不是JSF也不是Dao设计模式实现问题。这是一个基本的Java问题:未解决的编译问题。确保包DAOException内有一个mybeans类(或者这个例外的包应该是)。

基本样本:

package mybeans;

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

答案 1 :(得分:0)

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

DAOException抛出一个datalist.list()时,您需要处理可能的{{1}}。要么试一下try / catch,要么重新抛出异常