如何创建dao层 和dao层的实现?

背景
父类 :BaseDao.java 提供 所有表格都要执行的共同操作方法:增删改查,这样减少代码的重复
接口: NewsDao.java 为一个表格对应的接口,这样降低代码的耦合性
实现类: NewsDaoImpl.java 实现接口
连接数据库的方法:直接写在父类业务类中;

在main方法内对各个功能进行测试时,直接传实参,实参也就是 数据表格的字段;

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;

public class BaseDao {
	//实现增删改查的功能  以及连接 和释放资源
	Connection cnt=null;
	PreparedStatement ps=null;
	ResultSet rs=null;
	
	//连接有3种方式
	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 (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException 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<params.length;i++){
					ps.setObject(i+1,params[i] );
				}
				//执行 增删改,返回影响的行数
				updateRows=ps.executeUpdate();
				
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		return updateRows;
		
	}

	//查询 全部信息 或者指定部分信息
	public ResultSet ExecuteQuery(String sql ,Object [] params){
		if(this.getConnection()){
			try {
				ps=cnt.prepareStatement(sql);
				//给占位符赋值
				for(int i=0;i<params.length;i++){
					ps.setObject(i+1, params[i]);
				}
				// 执行查询,返回值是ResultSet型的
				rs=ps.executeQuery();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return rs;
	}
	
	

}

NewsDao.java

package com.kgc.dao;

import java.util.Date;
   //接口类
public interface NewsDao {
	//查询全部信息
	public void getList();
	
	//查询部分信息
	public void getNewsTitle(String title);
	
	//增
	public void add(int id,int categoryId,String title,
			String summary,String content,Date createDate);
	//删
	public void delete(int id);
	//改
	public void update(int id, String title);
	
}

NewsDaoImpl.java

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;

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(String title) {
		// 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={title};
		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(int id, int categoryId, String title, String summary,
			String content, Date createDate) {
		// 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};
		int i=this.executeUpdate(sql, params);
		if(i>0){
			System.out.println("添加成功");
		}
	}

	//删除
	@Override
	public void delete(int id) {
		// TODO Auto-generated method stub
		String sql="delete from news_detail where id=?";
		Object [] params={id};
		int i=this.executeUpdate(sql, params);
		if(i>0){
			System.out.println("删除成功");
		}
	}
	
//修改
	@Override
	public void update(int id,String title) {
		// TODO Auto-generated method stub
		String sql="update news_detail set title=? where id=?";
		Object [] params={title,id};
		int i=this.executeUpdate(sql, params);
		if(i>0){
			System.out.println("修改成功");
			
		}
	}
	
	public static void main(String[] args) {
		//向上转型 
		NewsDao newsDao=new NewsDaoImpl();
		
		//newsDao.getList();
		//newsDao.getNewsTitle("%课%");
		//newsDao.update(1, "我是2");
		//newsDao.add(6, 1, "a", "b", "c", new Timestamp(new Date().getTime()));
		//newsDao.delete(6);
		
	}

}




已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 精致技术 设计师:CSDN官方博客 返回首页