BaseDao.java
package com.kgc.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
import com.kgc.util.ConfigManager;
public class BaseDao {
//实现增删改查的功能 以及连接 和释放资源
Connection cnt=null;
PreparedStatement ps=null;
ResultSet rs=null;
public boolean getConnection(){
String driver="com.mysql.jdbc.Driver";
String url="jdbc:mysql://localhost:3306/kgcnews";
String username="root";
String password="root";
try {
Class.forName(driver);
cnt=DriverManager.getConnection(url,username,password);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return true;
}
//关闭资源
public boolean closeResource(){
if(rs!=null){
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(ps!=null){
try {
ps.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(cnt!=null){
try {
cnt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return true;
}
//增删改
public int executeUpdate(String sql ,Object [] params){
int updateRows=0;
if(this.getConnection()){
//获得sql语句
try {
ps=cnt.prepareStatement(sql);
//给占位符的赋值
for(int i=0;i
package com.kgc.dao;
import com.kgc.pojo.NewsCategory;
public interface NewsCategoryDao {
//删除
public boolean deleteNewsCategoryById(NewsCategory nc);
}
package com.kgc.dao;
import java.util.Date;
import com.kgc.pojo.News;
import com.kgc.pojo.NewsCategory;
//接口类
public interface NewsDao {
//查询全部信息
public void getList();
//查询部分信息
public void getNewsTitle(News news);
//增
public void add(News news);
//public void add(int id,int categoryId,String title,
//String summary,String content,Date createDate);
//删
public void delete(News news);
//改
public void update(News news);
//统计 新闻分类被使用次数
public int getCountCategoryIdByNewsDetail(NewsCategory nc);
}
package com.kgc.dao.impl;
import com.kgc.dao.BaseDao;
import com.kgc.dao.NewsCategoryDao;
import com.kgc.pojo.NewsCategory;
public class NewsCategoryDaoImpl extends BaseDao implements NewsCategoryDao {
//删除
@Override
public boolean deleteNewsCategoryById(NewsCategory nc) {
// TODO Auto-generated method stub
String sql="delete from news_category where id=?";
Object [] params={nc.getId()};
if(this.getConnection()){
int i=this.executeUpdate(sql, params);
if(i>0){
System.out.println("删除成功");
}
}
this.closeResource();
return true;
}
}
package com.kgc.dao.impl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Date;
import com.kgc.dao.BaseDao;
import com.kgc.dao.NewsDao;
import com.kgc.pojo.News;
import com.kgc.pojo.NewsCategory;
public class NewsDaoImpl extends BaseDao implements NewsDao{
ResultSet rs=null;
Connection cnt=null;
PreparedStatement ps=null;
//查询全部信息
@Override
public void getList() {
// TODO Auto-generated method stub
String sql="select id, categoryId,title,summary, content,picPath,author,createDate,modifyDate from news_detail";
//初始化 数组 ,避免空指针
Object[] params={};
rs=this.ExecuteQuery(sql, params);
//在控制台输出 rs的结果集
try {
while(rs.next()){
int id=rs.getInt(1);
int categoryId=rs.getInt(2);
String title=rs.getString("title");
String summary=rs.getString(4);
String content=rs.getString(5);
String picPath=rs.getString(6);
String author=rs.getString(7);
Date createDate=rs.getDate(8);
Date modifyDate=rs.getDate(9);
System.out.println(id+"\t"+categoryId+"\t"+title+"\t"+summary+"\t"+content+"\t"+picPath+"\t"+author+"\t"+createDate+"\t"+modifyDate );
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
this.closeResource();
}
}
//查询部分信息
@Override
public void getNewsTitle(News news) {
// TODO Auto-generated method stub
//返回值是ResultSet类型
String sql="select id, categoryId,title,summary, content,picPath,author,createDate,modifyDate from news_detail where title like ?";
Object [] params={news.getTitle()};
rs=this.ExecuteQuery(sql, params);
//在控制台遍历出 结果集
try {
while(rs.next()){
int id=rs.getInt(1);
int categoryId=rs.getInt(2);
String title1=rs.getString("title");
String summary=rs.getString(4);
String content=rs.getString(5);
String picPath=rs.getString(6);
String author=rs.getString(7);
Date createDate=rs.getDate(8);
Date modifyDate=rs.getDate(9);
System.out.println(id+"\t"+categoryId+"\t"+title1+"\t"+summary+"\t"+content+"\t"+picPath+"\t"+author+"\t"+createDate+"\t"+modifyDate );
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
this.closeResource();
}
}
//增
@Override
public void add(News news) {
// TODO Auto-generated method stub
String sql="insert into news_detail(id, categoryId, title, summary,content, createDate) values(?,?,?,?,?,?)";
//Object [] params={id, categoryId, title, summary,content, createDate};
Object[] params={news.getId(),news.getCategoryId(),news.getTitle(),news.getSummary()
,news.getContent(),news.getCreateDate()};
int i=this.executeUpdate(sql, params);
if(i>0){
System.out.println("添加成功");
}
}
//删除
@Override
public void delete(News news) {
// TODO Auto-generated method stub
String sql="delete from news_detail where id=?";
Object [] params={news.getId()};
int i=this.executeUpdate(sql, params);
if(i>0){
System.out.println("删除成功");
}
}
//修改
@Override
public void update(News news) {
// TODO Auto-generated method stub
String sql="update news_detail set title=? where id=?";
Object [] params={news.getTitle(),news.getId()};
int i=this.executeUpdate(sql, params);
if(i>0){
System.out.println("修改成功");
}
}
//统计 新闻分类被使用次数
@Override
public int getCountCategoryIdByNewsDetail(NewsCategory nc) {
// TODO Auto-generated method stub
int i=0;
String sql="SELECT COUNT(*) FROM news_detail WHERE categoryId=?";
Object [] params={nc.getId()};
if(this.getConnection()){
rs=this.ExecuteQuery(sql, params);
try {
while(rs.next()){
i=rs.getInt(1);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
this.closeResource();
}
}
return i;
}
}
package com.kgc.pojo;
import java.util.Date;
public class News {
private int id;
private int categoryId;
private String title;
private String summary;
private String content;
private String picPath;
private String author;
private Date createDate;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getCategoryId() {
return categoryId;
}
public void setCategoryId(int categoryId) {
this.categoryId = categoryId;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getSummary() {
return summary;
}
public void setSummary(String summary) {
this.summary = summary;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getPicPath() {
return picPath;
}
public void setPicPath(String picPath) {
this.picPath = picPath;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public Date getCreateDate() {
return createDate;
}
public void setCreateDate(Date createDate) {
this.createDate = createDate;
}
}
package com.kgc.pojo;
import java.util.Date;
public class NewsCategory {
private int id;
private String name;
private Date createDate;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getCreateDate() {
return createDate;
}
public void setCreateDate(Date createDate) {
this.createDate = createDate;
}
}
package com.kgc.service;
import com.kgc.pojo.NewsCategory;
public interface NewsCategoryService {
//根据新闻信息是否存在该分类决定是否删除该分类
public boolean deleteNewsCategoryById(NewsCategory nc);
}
package com.kgc.service.impl;
import com.kgc.dao.NewsCategoryDao;
import com.kgc.dao.NewsDao;
import com.kgc.dao.impl.NewsCategoryDaoImpl;
import com.kgc.dao.impl.NewsDaoImpl;
import com.kgc.pojo.NewsCategory;
import com.kgc.service.NewsCategoryService;
public class NewsCategoryServiceImpl implements NewsCategoryService {
NewsCategoryDao ncd=new NewsCategoryDaoImpl();
NewsDao nd=new NewsDaoImpl();
//根据新闻信息是否存在该分类决定是否删除该分类
public boolean deleteNewsCategoryById(NewsCategory nc){
//判断这个分类是否被使用,如果使用了,就不能删除,若是没有被使用,可以直接删除
int i=nd.getCountCategoryIdByNewsDetail(nc);
if(i>0){
System.out.println("该分类不能删除");
}else{
//执行删除
ncd.deleteNewsCategoryById(nc);
System.out.println("删除成功");
}
return false;
}
}
package com.kgc.service.impl;
import com.kgc.pojo.NewsCategory;
import com.kgc.service.NewsCategoryService;
public class Test {
public static void main(String[] args) {
NewsCategoryService ncs=new NewsCategoryServiceImpl();
NewsCategory nc=new NewsCategory();
nc.setId(6);
ncs.deleteNewsCategoryById(nc);
}
}