CMS简单内容管理系统

架构

 

 

 

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

}

 

 

posted @ 2018-06-18 16:06  H丶  阅读(896)  评论(0编辑  收藏  举报