1. 程式人生 > >CMS簡單內容管理系統

CMS簡單內容管理系統

close author driver upload getc server style dsta seda

架構

技術分享圖片

NewsDaoSQLServerImpl

public class NewsDaoSQLServerImpl extends BaseDao implements NewsDao {

    public void testSelect() throws Exception {
        List<News> list = getAllNews();
        for (News news : list) {
            System.out.println(news.getTITLE());
            System.out
.println(news.getAUTHOR()); System.out.println(news.getCREATETIME()); System.out.println(news.getCONTENT()); } } @Override public List<News> getAllNews() throws Exception { List<News> list = new ArrayList<News>(); String sql
= "select * from book"; // 讀取器 ResultSet rs = executeQuery(sql); if (rs != null) { while (rs.next()) { // 如果有數據,數據表中的每條記錄對應實體類的一個實例 News news = new News(); news.setAUTHOR(rs.getString("AUTHOR"));//作家 news.setCREATETIME(rs.getString("
CREATETIME"));//創建時間 news.setTITLE(rs.getString("TITLE"));//標題 news.setCONTENT(rs.getString("CONTENT"));//內容 list.add(news); } } return list; } }

NewsDao

public interface NewsDao {
    // 讀取所有新聞列表的方法
    public List<News> getAllNews() throws Exception;
}

NewsManager

public class NewsManager {
    
    public void toHtml() throws Exception {
        // 讀取模板文件內容,返回文件內容的字符串
        FileIO fileio = new FileIO();
        String templaterstr = fileio.readFile("E:\\news\\news.template");
        // 讀取數據庫表,獲取新聞列表
        NewsDao newsdao = new NewsDaoSQLServerImpl();
        List<News> newslist = newsdao.getAllNews();
        // 替換模板文件,為每一條新聞創建一個HTML文件顯示其信息
        for (int i = 0; i < newslist.size(); i++) {
            // 獲取一條新聞
            News news = newslist.get(i);
            // 使用該條新聞信息替換對應的占位符
            String replacestr = new String();
            replacestr = templaterstr;
            replacestr = replacestr.replace("{title}", news.getTITLE());
            replacestr = replacestr.replace("{author}", news.getAUTHOR());
            replacestr = replacestr.replace("{createtime}", news.getCREATETIME());
            replacestr = replacestr.replace("{content}", news.getCONTENT());
            // 為該條新聞生成HTML文件
            String filePath = "E:\\news\\news" + i + ".html";
            fileio.writeFile(filePath, replacestr);
        }
    }
}

News

public class News {
    // ID, TITLE, AUTHOR, CREATETIME, CONTENT
    private int ID;// 新聞的ID
    private String TITLE;// 新聞標題
    private String AUTHOR;// 新聞的作者
    private String CREATETIME;// 時間
    private String CONTENT;// 新聞的內容

    public int getID() {
        return ID;
    }

    public void setID(int iD) {
        ID = iD;
    }

    public String getTITLE() {
        return TITLE;
    }

    public void setTITLE(String tITLE) {
        TITLE = tITLE;
    }

    public String getAUTHOR() {
        return AUTHOR;
    }

    public void setAUTHOR(String aUTHOR) {
        AUTHOR = aUTHOR;
    }

    public String getCREATETIME() {
        return CREATETIME;
    }

    public void setCREATETIME(String cREATETIME) {
        CREATETIME = cREATETIME;
    }

    public String getCONTENT() {
        return CONTENT;
    }

    public void setCONTENT(String cONTENT) {
        CONTENT = cONTENT;
    }

}

BaseDao

public class BaseDao {
    // 定義四個靜態常量,保存數據連接信息
    private static  String driver="com.mysql.jdbc.Driver";
    private static  String url="jdbc:mysql://localhost:3306/newsmgr";
    private static  String username="root";
    private static  String password = "";

    Connection con;
    PreparedStatement stat;
    
    

    

    // 獲取連接對象的方法
    public Connection getConnection() throws Exception {
        Class.forName(driver);
        if (con == null || con.isClosed()) {
            con = DriverManager.getConnection(url, username, password);
        }
        return con;
    }

    // 對所有select語句執行的方法
    public ResultSet executeQuery(String sql, Object... objs) throws Exception {
        con = getConnection();
        stat = con.prepareStatement(sql);
        for (int i = 0; i < objs.length; i++) {
            stat.setObject(i + 1, objs[i]);
        }
        ResultSet rs = stat.executeQuery();
        return rs;
    }
}

Test

public class Test {

    public static void main(String[] args) throws Exception {
        NewsManager num = new NewsManager();
        num.toHtml();
    }
    
    

}

技術分享圖片

CMS簡單內容管理系統