【JDBC-4】关于DAO

【JDBC-4】关于DAO,第1张

【JDBC-4】关于DAO

baseDAO:

package guo_DAO_2;

import JDBC_Utils.JDBCUtils;

import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;


public abstract class baseDAO {
    private Class clazz=null;

    {
        //获取当前对象的带泛型的父类,此处的this指的不是baseDAO,而是CustomerDAO
        Type superclass = this.getClass().getGenericSuperclass();
        ParameterizedType paramType= (ParameterizedType) superclass;
        Type[] arguments = paramType.getActualTypeArguments();//获取父类的泛型参数
        clazz= (Class) arguments[0];//获取了泛型的第一个参数
    }

    //通用的增删改 *** 作(考虑事务)version2.0
    public void update(Connection conn, String sql, Object ...args){//使用可变形参
        PreparedStatement ps = null;
        try {
            //sql当中占位符的个数应该与可变形参的长度一致
            //1.获取数据库的连接
            ps = conn.prepareStatement(sql);
            //2.填充占位符
            for(int i=0;i< args.length;i++)
            {
                ps.setObject(i+1,args[i]);
            }
            //3.执行操作
            ps.execute();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //4.资源关闭
            JDBCUtils.CloseResource(null,ps);
        }
    }



    
    public T getInstance(Connection conn,String sql,Object...args){
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = conn.prepareStatement(sql);
            for(int i=0;i getForList(Connection conn, String sql, Object...args){
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = conn.prepareStatement(sql);
            for(int i=0;i ts = new ArrayList();
            while(rs.next()){
                T t = clazz.newInstance();
                //要给customer赋值为取得的某个属性
                //处理结果集一行数据中的每一个列
                for(int i=0;i E getValue(Connection conn,String sql,Object...args)  {
        PreparedStatement ps=null;
        ResultSet rs=null;
        try {
            ps = conn.prepareStatement(sql);
            for(int i=0;i

CustomerDAO(Interface)

package guo_DAO_2;

import guo_Bean.Customer;

import java.sql.Connection;
import java.sql.Date;
import java.util.List;


public interface CustomerDAO {
    
    void insert(Connection conn, Customer cust);

    
    void deleteByID(Connection conn,int id);

    //针对内存中的cust对象去修改数据表中的记录
    void updateByID(Connection conn,Customer cust);

    Customer getCustomerByID(Connection conn,int id);

    List getAll(Connection conn);

    Long getCount(Connection conn);

    Date getMaxBirth(Connection conn);
}
package guo_DAO_2;

import guo_Bean.Customer;

import java.sql.Connection;
import java.sql.Date;
import java.util.List;



public class CustomerDAOImpl extends baseDAO implements CustomerDAO {
    @Override
    public void insert(Connection conn, Customer cust) {
        String sql= "insert into customers(name,email,birth) values(?,?,?) ";
        update(conn,sql,cust.getName(),cust.getEmail(),cust.getBirth());
    }

    @Override
    public void deleteByID(Connection conn, int id) {
        String sql="delete from customers where id =?";
        update(conn,sql,id);
    }

    @Override
    public void updateByID(Connection conn,Customer cust) {
        String sql= "update customers set name =?,email=?,birth=? where id =?";
        update(conn,sql,cust.getName(),cust.getEmail(),cust.getBirth(),cust.getId());
    }

    @Override
    public Customer getCustomerByID(Connection conn, int id) {
        String sql="select id,name,email,birth from customers where id =?";
        Customer customer = getInstance(conn, sql, id);
        return customer;
    }

    @Override
    public List getAll(Connection conn) {
        String sql="select id,name,email,birth from customers";
        List list = getForList(conn, sql);
        return list;
    }

    @Override
    public Long getCount(Connection conn) {
        String sql="select count(*) from customers";
        return getValue(conn, sql);
    }

    @Override
    public Date getMaxBirth(Connection conn) {
        String sql="select max(birth)from customers";
        return getValue(conn,sql);
    }
}

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/5713272.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-12-18
下一篇2022-12-17

发表评论

登录后才能评论

评论列表(0条)

    保存