• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
﹏℡幸福?
博客园    首页    新随笔    联系   管理    订阅  订阅

JavaOOP项目 CMS内容管理系统

数据库里创建一个News表,要有标题、作者、时间、内容等列。

1:首先要使用JDBC进行数据库连接,得先在项目里新建一个Folder,把Sqlserver 的驱动jar包导入。

2:使用MyEclipse创建一个HTML模板文件:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

  <head>

    <title>{title}</title>

      <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">

    <meta http-equiv="description" content="this is my page">

    <meta http-equiv="content-type" content="text/html; charset=gb2312">

        <!--<link rel="stylesheet" type="text/css" href="./styles.css">-->

  </head>

    <body>

    <table align="center" width="95%" border="1">

      <tr>

         <td width="10"><b>标题:</b></td>

         <td>{title}</td>

      </tr>

      <tr>

         <td width="10"><b>作者:</b></td>

         <td>{author}</td>

      </tr>

      <tr>

         <td width="10"><b>时间:</b></td>

         <td>{creataeTime}</td>

      </tr>

      <tr>

         <td width="10"><b>内容:</b></td>

         <td>{content}</td>

      </tr>

    </table> 

  </body>

</html>

2:在创建dao包、dao.impl包、entity包、manager包、test包、util包

在entity包里创建实体类(数据库相对应的实体类的字段):

package cn.cms.entity;

    private String title;
     private String author;
     private String tiem;
     private String content;

01:在dao包里创建数据DataDao类来和数据库交互:

 //定义驱动字符串
 static String driver="com.microsoft.sqlserver.jdbc.SQLServerDriver";
 static String url="jdbc:sqlserver://localhost:1433;DatabaseName=MyNews";
 static String user="sa";
 static String password="";

Connection con=null;
 PreparedStatement ps=null;
 ResultSet rs=null;

//创建一个方法返回连接字符串
 public Connection getConnection(){
  //加载驱动
  try {
   Class.forName(driver);
   if(con==null){
    con=DriverManager.getConnection(url, user, password);
   }
  } catch (Exception e) {
   e.printStackTrace();
  }
  return con;
 }

 

//创建一个返回查询结果的方法
 public ResultSet executeQuery(String sql,Object...paras){
  //获取连接字符串
  con=getConnection();
  try {
   //执行SQL语句
   ps=con.prepareStatement(sql);
   //给参数赋值
   if(paras.length>0){
    for (int i = 0; i < paras.length; i++) {
                    ps.setObject(i+1, paras[i]);      
    }
   }
   //返回结果
   rs=ps.executeQuery();
  } catch (Exception e) {
   e.printStackTrace();
  }
 
  return rs;
 }

 

//创建一个返回增删改结果的方法
 public int executeUpdate(String sql,Object...paras){
  int count=0;
  //获取连接字符串
  con=getConnection();
  try {
   //执行SQL语句
   ps=con.prepareStatement(sql);
   //给参数赋值
   if(paras.length>0){
    for (int i = 0; i < paras.length; i++) {
     ps.setObject(i+1,paras[i]);
    }
   }
   count=ps.executeUpdate();
  } catch (Exception e) {
   // TODO: handle exception
  }
  return count;
 }
 

//创建一个关闭所有连接的方法
 public void closeResource(){
  try {
   if(rs!=null){
    rs.close();
   }
   if(ps!=null){
    ps.close();
   }
   if(con!=null){
    con.close();
   }
  } catch (Exception e) {
   e.printStackTrace();
  }
 }

 

02:再在dao包里创建一个接口类:

import java.util.List;

import cn.cms.entity.News;

public interface NewsDao {

     List<News> getNews();

}

03:再在impl包里定义一个实现类继承自DataBase实现接口类News;

public class NewsImpl extends BaseDao implements NewsDao {

 @Override  public List<News> getNews() {

  List<News> list=new ArrayList<News>();

  String sql="select title,author,time,content from news";

  ResultSet rs=executeQuery(sql);    

     try {

                while(rs.next()){

       News news=new News();

       news.setTitle(rs.getString(1));

       news.setAuthor(rs.getString(2));

       news.setTiem(rs.getString(3));

       news.setContent(rs.getString(4));

       list.add(news);

      }  

       closeResource();

  } catch (Exception e) {

   e.printStackTrace();

  }  

 return list;

 }

04:再在util工具包里创建一个FileIO类,里面有两个方法,一读,一写;

public class FileIO {
   //读取文件的方法

 public String  ReaderFile(String filePath){
    FileInputStream fs=null;
    StringBuffer sb=new StringBuffer();
    try {
  fs=new FileInputStream(filePath);
  byte[] bytes=new byte[1024];
  
  int size=0;
  while((size=fs.read(bytes))!=-1){
   String str=new String(bytes);
   sb.append(str);
  }
 } catch (Exception e) {
  e.printStackTrace();
 }
    return sb.toString();
   }

 

 //写入文件的方法
   public void WriterFile(String newPath,String str){
    FileOutputStream fos=null;
    try {
  fos=new FileOutputStream(newPath);
  byte[] words=str.getBytes();
  fos.write(words,0,words.length);
 } catch (Exception e) {
  e.printStackTrace();
 }finally{
  try {
   if(fos!=null){
    fos.close();
   }
  } catch (Exception e2) {
   // TODO: handle exception
  }
 }

}

05:再在manager包里创建一个类,里面有一个把数据转换成HTML的方法;

public class NewsManager {
      public void toHtml(){
       //读取模板文件内容,返回文件内容字符串
     FileIO io=new FileIO();
     String str=io.ReaderFile("C:\\Documents and Settings\\Administrator\\Workspaces\\MyEclipse 8.5\\MyCMS\\src\\news.template.html");

//读取数据库列表,返回新闻列表
     NewsDao newsDao=new NewsImpl();
     List<News> list=newsDao.getNews();
     //替换模板文件,为每一条新闻创建一个HTML文件来显示其信息
     for (int i = 0; i < list.size(); i++) {
      //获取一条新闻
   News newes=list.get(i);
   //使用该条新闻信息替换对应占位符
   String newStr=new String();
   newStr=str;
   newStr=newStr.replace("{title}", newes.getTitle());
   newStr=newStr.replace("{author}", newes.getAuthor());
   newStr=newStr.replace("{creataeTime}", newes.getTiem());
   newStr=newStr.replace("{content}",newes.getContent());
   String newPath="D:\\news"+i+".html";
   io.WriterFile(newPath, newStr);
   System.out.println("第"+i+"条新闻完成!");
  }
     
      }
}

06:最后在test包里创建一个带main方法的类:

  NewsManager nm=new NewsManager();
  nm.toHtml();

进行测试;

posted @ 2014-07-06 07:41  ﹏℡幸福?  阅读(739)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3