我是這樣理解DAO的,對嗎?

chinahero發表於2003-09-04
我在寫一個WEB的輕型應用,想應用DAO,我想DAO模式的主要目的是,使具體的資料庫SQL語句與應用分離,我是這樣用的,是不是DAO模式
1.INewsDao 介面

public interface INewsDao {
public List getNews(int count,Date today);
...
}
2.NewsDaoOracleImpl 是對應Oracle資料庫的一個實現

public class NewsDaoOracleImpl implements INewsDao {
private static NewsHome home=new NewsHome(); //EJB或JDO物件
public List getNews(int count, Date today) {
String sql="select * from news where ...";
List myList=home.getNews(sql); //EJB或JDO呼叫
return myList;
}
}

3.NewsDaoFactory 類工廠public class NewsDaoFactory {

//返回INewsDao介面型別
public static INewsDao getDao() {
INewsDao newdao = null;

try {
newdao = (INewsDao) Class.forName(
"my.dao.NewsDaoOracleImpl")
.newInstance();
} catch (ClassNotFoundException ce) {
System.out.println("get News Dao ClassNot Found");
ce.printStackTrace();
System.out.println("get News Dao ClassNot Found end...");
} catch (Exception ce) {
System.out.println("get News Dao Exception");
ce.printStackTrace();
System.out.println("get News Dao Exception end...");
}

return newdao;
}
}

多謝!

相關文章