Java oop 第十一章 CMS
来源:互联网 发布:2017网络情歌 编辑:程序博客网 时间:2024/06/06 00:42
package cn.jbit.cms.dao;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;public class BaseDao {Connection con = null;PreparedStatement ps = null; public ResultSet rs=null;private static final String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";private static final String url = "jdbc:sqlserver://localhost:1433;DatabaseName=SQLServer2008";private static final String username = "sa";private static final String pwd = "1";public Connection getCon()throws Exception{Class.forName(driver);if(con==null||con.isClosed()){ con = DriverManager.getConnection(url, username, pwd);}return con;}//执行查询public ResultSet executeQuery(String sql,Object... obje) throws Exception{con=getCon();ps=con.prepareStatement(sql);for(int i=0;i<obje.length;i++){ps.setObject(i+1, obje[i]);}rs=ps.executeQuery();return rs;}//执行修改public int execteUpdate(String sql,Object...object) throws Exception{con=getCon();ps=con.prepareStatement(sql);ps.executeUpdate();for(int i=0;i<object.length;i++){ps.setObject(i+1, object[i]);}int count=ps.executeUpdate();return count;}//执行增加public int execteadd(String sql,Object...object) throws Exception{con=getCon();ps=con.prepareStatement(sql);ps.executeUpdate();for(int i=0;i<object.length;i++){ps.setObject(i+1, object[i]);}int count=ps.executeUpdate();return count;}//执行删除public int exectedelete(String sql,Object...object) throws Exception{con=getCon();ps=con.prepareStatement(sql);ps.executeUpdate();for(int i=0;i<object.length;i++){ps.setObject(i+1, object[i]);}int count=ps.executeUpdate();return count;}public void closeAll() throws Exception{if(rs!=null){rs.close();}if(ps!=null){ps.close();}if(con!=null){con.close();}}//}
package cn.jbit.cms.dao;import java.util.List;import cn.jbit.cms.entity.News;public interface NewsDao { public List<News> findAll() throws Exception;}
package cn.jbit.cms.dao.impl;import java.sql.ResultSet;import java.util.ArrayList;import java.util.List;import cn.jbit.cms.dao.BaseDao;import cn.jbit.cms.dao.NewsDao;import cn.jbit.cms.entity.News;public class NewsDaoSQLServerImpl extends BaseDao implements NewsDao{@Overridepublic List<News> findAll() throws Exception{List<News> list=new ArrayList<News>();String sql="select * from news";ResultSet rs=this.executeQuery(sql);if(rs!=null){while(rs.next()){News news=new News();news.setTitle(rs.getString("Title"));news.setAuthor(rs.getString("Author"));news.setDate(rs.getString("Date"));news.setBody(rs.getString("Body")); list.add(news); }}return list;}}
package cn.jbit.cms.entity;public class News {private int id;public int getId() {return id;}public void setId(int id) {this.id = id;}public int getTypeid() {return typeid;}public void setTypeid(int typeid) {this.typeid = typeid;}private String title;private String author;private String date;private String body;private int typeid;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 String getDate() {return date;}public void setDate(String date) {this.date = date;}public String getBody() {return body;}public void setBody(String body) {this.body = body;}public News(int id, String title, String author, String date, String body,int typeid) {super();this.id = id;this.title = title;this.author = author;this.date = date;this.body = body;this.typeid = typeid;}public News() {super();// TODO Auto-generated constructor stub}}
package cn.jbit.cms.manager;import java.util.List;import cn.jbit.cms.dao.NewsDao;import cn.jbit.cms.dao.impl.NewsDaoSQLServerImpl;import cn.jbit.cms.entity.News;import cn.jbit.cms.util.FileIO;public class NewsManager {public void Tohtml()throws Exception{FileIO fileio=new FileIO();String templatestr=fileio.readFile("E:\\news.template");NewsDao ndao=new NewsDaoSQLServerImpl();List<News> newList=ndao.findAll();for(int i=0; i<newList.size();i++){News news=newList.get(i);String replacestr=new String();replacestr=templatestr;replacestr=replacestr.replace("{title}", news.getTitle());replacestr=replacestr.replace("{author}", news.getAuthor());replacestr=replacestr.replace("{createTime}", news.getDate());replacestr=replacestr.replace("{content}", news.getBody());String filePath="E:\\news"+i+".html";fileio.writrFile(filePath,replacestr);}}}
package cn.jbit.cms.test;import cn.jbit.cms.manager.NewsManager;public class Test {public static void main(String[] args) throws Exception{NewsManager news =new NewsManager();news.Tohtml();}}
package cn.jbit.cms.util;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.InputStream;import java.io.InputStreamReader;import java.io.OutputStreamWriter;public class FileIO {public String readFile(String filePath) throws Exception {String place="";File file=new File(filePath);try{InputStream is=new FileInputStream(file);InputStreamReader input=new InputStreamReader(is,"gb2312");BufferedReader reader=new BufferedReader(input);String line;while((line=reader.readLine())!=null){place+=line;}is.close();}catch(Exception e){e.printStackTrace();}return place;}public void writrFile(String filePath, String replacestr) throws Exception {File file=new File(filePath);FileOutputStream out;try{out=new FileOutputStream(file);OutputStreamWriter input=new OutputStreamWriter(out, "gb2312");BufferedWriter bw=new BufferedWriter(input);bw.write(replacestr);bw.close();}catch(Exception e){e.printStackTrace();}}}
0 0
- Java oop 第十一章 CMS
- java第十一章
- JAVA第十一章复习
- JAVA OOP 第二章继承
- JAVA OOP第三章 多态
- JAVA OOP 第四章 接口
- JAVA OOP第六章 异常
- java oop 第十章 IO
- java oop 第十二章宠物商店
- java语言程序设计 第十一章 11.3
- java OOP
- Java OOP
- JAVA OOP
- JAVA-OOP
- Java OOP
- Java OOP
- java OOP
- java-oop
- 快速幂运算
- 未来已来——十五年网龄生活洞见科技之飞跃
- 常用排序算法总结
- spring-boot整合redis作为缓存(4)——spring-boot引入Redis
- python kmeans实战
- Java oop 第十一章 CMS
- 使用ffmpeg步骤
- 类的无参方法
- 回调函数实现
- Cocos2d-js 触摸事件的简单封装
- MVC框架简介,SSH 和SSM
- 怎么再vue中使用jquery?
- 反射_通过反射写一个通用的设置某个对象的某个属性为指定的值
- AndroidStudio NDK的接入填坑记