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{
@Override
public 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();
}
}
}