create table NEWS ( ID INTEGER not null, TITLE VARCHAR2(50), AUTHOR VARCHAR2(12), CREATETIME DATE, CONTENT VARCHAR2(2000) ) insert into NEWS (ID, TITLE, AUTHOR, CREATETIME, CONTENT) values (1, 'title1', 'author1', to_date('14-06-2010', 'dd-mm-yyyy'), 'content1'); insert into NEWS (ID, TITLE, AUTHOR, CREATETIME, CONTENT) values (2, 'title2', 'author2', to_date('21-06-2010', 'dd-mm-yyyy'), 'content2'); insert into NEWS (ID, TITLE, AUTHOR, CREATETIME, CONTENT) values (3, 'title3', 'author3', to_date('23-06-2010', 'dd-mm-yyyy'), '内容3'); commit;
entity:
package cn.jbit.entity; import java.util.Date; public class News { // 新闻标识符 private int id; // 新闻标题 private String title; // 新闻作者 private String author; // 新闻创建时间 private Date createTime; // 新闻内容 private String content; /** * 构造方法 */ public News() { } public News(int id, String title, String author, Date createTime, String content) { this.setId(id); this.setTitle(title); this.setAuthor(author); this.setCreateTime(createTime); this.setContent(content); } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } public Date getCreateTime() { return createTime; } public void setCreateTime(Date createTime) { this.createTime = createTime; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } }
dao:
package cn.jbit.dao; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; public class BaseDao { // 数据库驱动字符串 private static String driver = "oracle.jdbc.driver.OracleDriver"; // 连接URL字符串 private static String url = "jdbc:oracle:thin:@localhost:1521:oracle10"; // 数据库用户名 private static String user = "news"; // 用户密码 private static String password = "accp"; /** * 获取数据库连接对象 */ public Connection getConnection() { Connection conn = null; // 数据连接对象 // 获取连接并捕获异常 try { // 加载驱动 Class.forName(driver); // 获取数据库连接 conn = DriverManager.getConnection(url, user, password); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } return conn; // 返回数据连接对象 } /** * 关闭数据库连接 */ public void closeAll(Connection conn, PreparedStatement pstmt, ResultSet rs) { // 若结果集对象不为空,则关闭 if (rs != null) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } } // 若PreparedStatement对象不为空,则关闭 if (pstmt != null) { try { pstmt.close(); } catch (SQLException e) { e.printStackTrace(); } } // 若数据库连接对象不为空,则关闭 if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } /** * 执行更新、删除、插入SQL语句命令 */ public int executeUpdate(String sql, Object[] pars) { int rowCount = 0; Connection conn = null; PreparedStatement pstmt = null; try { // 连接数据库 conn = this.getConnection(); // 创建数据库命令对象 pstmt = conn.prepareStatement(sql); if (pars != null) { for (int i = 0; i < pars.length; i++) { pstmt.setObject(i + 1, pars[i]); } } // 执行数据库命令 rowCount = pstmt.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } return rowCount; } }
package cn.jbit.dao; import java.util.List; import cn.jbit.entity.News; public interface NewsDao { /** * 对News进行查询操作 */ List<News> executeQuery(String sql, Object[] pars); /** * 对News进行增、删、改操作 */ int executeUpdate(String sql, Object[] pars); }
实现dao:
package cn.jbit.dao.impl; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import cn.jbit.dao.BaseDao; import cn.jbit.dao.NewsDao; import cn.jbit.entity.News; public class NewsDaoOraleImpl extends BaseDao implements NewsDao { Connection conn = null; PreparedStatement pstmt = null; ResultSet rs = null; /** * 查询所有新闻信息 */ public List<News> executeQuery(String sql, Object[] param) { // 创建一个泛型集合对象 List<News> newLists = new ArrayList<News>(); try { // 获取数据库连接对象 conn = super.getConnection(); // 创建数据库命令对象 pstmt = conn.prepareStatement(sql); if (param != null) { for (int i = 0; i < param.length; i++) { pstmt.setObject((i + 1), param[i]); } } // 执行数据库命令 rs = pstmt.executeQuery(); // 处理结果 while (rs.next()) { // 创建一个新闻对象 News news = new News(); news.setId(rs.getInt(1)); news.setTitle(rs.getString(2)); news.setAuthor(rs.getString(3)); news.setCreateTime(rs.getDate(4)); news.setContent(rs.getString(5)); // 把数据添加到集合中 newLists.add(news); } } catch (SQLException e) { e.printStackTrace(); } finally { // 调用关闭数据库连接 super.closeAll(conn, pstmt, rs); } return newLists; } }
mondel:
package cn.jbit.mondel; import java.util.List; import cn.jbit.dao.NewsDao; import cn.jbit.dao.impl.NewsDaoOraleImpl; import cn.jbit.entity.News; import cn.jbit.tool.FileIO; /** * 使用数据库内容替换模板文件,生成HTML文件 * */ public class NewsManager { public void toHtml() { // 读取模板文件内容 FileIO fileIO = new FileIO(); String templates = fileIO.readFile("contentCMS.template"); // 读取数据库表 NewsDao newsDao = new NewsDaoOraleImpl(); String sql = "select * from news"; List<News> list = newsDao.executeQuery(sql, null); // 替换模板文件 for (int i = 0; i < list.size(); i++) { // 获取一条新闻 News news = list.get(i); // 使用该条新闻信息替换对应占位符 String replace = new String(); replace = templates; replace = replace.replace("{title}", news.getTitle()) .replace("{author}", news.getAuthor()) .replace("{createTime}", news.getCreateTime().toString()) .replace("{content}", news.getContent()); String path = "news" + i + ".html"; fileIO.writeFile(path, replace); } } }
tool:
package cn.jbit.tool; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.Reader; import java.io.Writer; import java.nio.charset.Charset; /** * 文件流 * */ public class FileIO { /** * 读取文件 * */ public String readFile(String path) { StringBuffer sbf = new StringBuffer(); Reader reader = null; char[] ch = new char[1024]; // 读取模板文件内空到StringBuffer中 try { reader = new InputStreamReader(new FileInputStream(path), Charset.forName("UTF-8")); while (reader.read(ch) != -1) { sbf.append(ch); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try {// 关闭读取器 if (reader != null) { reader.close(); } } catch (IOException e) { e.printStackTrace(); } } return sbf.toString(); } /** * 写入文件 */ public void writeFile(String path, String content) { Writer writer = null; // 写入流 try { writer = new OutputStreamWriter(new FileOutputStream(path), Charset.forName("UTF-8")); writer.write(content); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try {// 关闭写入流 if (writer != null) { writer.close(); } } catch (IOException e) { e.printStackTrace(); } } } }
test:
package cn.jbit.test; import cn.jbit.mondel.NewsManager; public class Test { public static void main(String[] args) { //创建对象 NewsManager newsMan=new NewsManager(); // 调用方法 newsMan.toHtml(); } }
个人E-mail:chaoyi77@163.com