关于接口在J2EE编程中的小运用及一些体会

关于接口在J2EE编程中的小运用及一些体会


关于接口

接口是一些列方法的抽象,并没有给出具体的实现,需要特定的类去实现它,在J2EE编程中,通常分为三块,前台的显示,业务逻辑部分以及数据库访问部分,也就是DAO层。在DAO层,则可以定义一个接口,这样换底层是数据库的话,更改起来也比较方便

代码块

例如定义一个将产品存入数据库的接口ProductDAO.java,例如:

@requires_authorization
import java.sql.Timestamp;
import java.util.List;

import com.bandc.shopping.Product;


public interface ProductDAO {
    public List<Product> getProductlist();
    public boolean addProduct(Product p);
    public List<Product> findProducts(int id,
                                      String name,
                                      String descr,
                                      double lownormalprice,
                                      double highnormalprice,
                                      double lowmembershipprice,
                                      double highmembershiprice,
                                      Timestamp pdate,
                                      int categoryid);

    public void deleteProductByCategoryId(int categoryid);
    public void deleteProductById(int id);
    public boolean updateProduct(Product p);

}

这里定义了一个接口,然后定义一个类去实现它ProductMySQLDAO.java,这就是一个基于MySQL数据库的一个具体的实现

package com.bandc.shopping.DAO;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.*;

import com.bandc.shopping.Product;
import com.bandc.shopping.util.DB;


public class ProductMySQLDAO implements ProductDAO {

    public List<Product> getProductlist() {
        return null;
    }

    public boolean addProduct(Product p) {
        int id = p.getId();
        String name = p.getName();
        String descr = p.getDescr();
        double normalprice = p.getNormalprice();
        double memberprice = p.getMemberprice();
         Timestamp pdate =p.getPdate();
        int categoryid = p.getCategoryid();

        Connection conn =null;
        String sql = null;
        PreparedStatement pstmt = null; 
        try{
            conn = DB.getConn();
            if(id == -1){
                sql = "insert into product values(null,?,?,?,?,?,?)";
            }else{
                sql = "insert into product values("+id+",?,?,?,?,?,?)";
            }
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, name);
            pstmt.setString(2, descr);
            pstmt.setDouble(3, normalprice);
            pstmt.setDouble(4, memberprice);
            pstmt.setTimestamp(5, pdate);
            pstmt.setInt(6, categoryid);
            //System.out.println(sql);
            pstmt.executeUpdate();
            System.out.println("插入成功");

        }catch(SQLException e){
            e.printStackTrace();
            return false;
        }finally{
            DB.closePstmt(pstmt);
            DB.closeConn(conn);

        }   

//System.out.println("插入成功");
        return true;

    }

    public List<Product> findProducts(int id, String name, String descr,
            double lownormalprice, double highnormalprice,
            double lowmembershipprice, double highmembershiprice,
            Timestamp pdate, int categoryid) {
        // TODO Auto-generated method stub
        return null;
    }

    public void deleteProductByCategoryId(int categoryid) {
        // TODO Auto-generated method stub

    }

    public void deleteProductById(int id) {
        // TODO Auto-generated method stub

    }

    public boolean updateProduct(Product p) {
        // TODO Auto-generated method stub
        return false;
    }

    public List<Product> findProducts(int id, String name, String descr,
            double lownormalprice, double highnormalprice,
            double lowmembershipprice, double highmembershiprice,
            com.sun.jmx.snmp.Timestamp pdate, int categoryid) {
        // TODO Auto-generated method stub
        return null;
    }
    ```
然后在业务逻辑层,有一个关于产品的管理类ProductManager.java
``` python
package com.bandc.shopping;

import java.util.List;

import com.bandc.shopping.DAO.ProductDAO;
import com.bandc.shopping.DAO.ProductMySQLDAO;

public class ProductManager {
    ProductDAO dao = null;

    private static ProductManager pm = null;
    static{
        if(pm == null){
            pm = new ProductManager();
            pm.setDAO(new ProductMySQLDAO());//只要改变这一句话,并生成相应的数据库的DAO,那么改变就完成了
        }
    }
    public static ProductManager getInstance(){
        return pm;
    }

    public List<Product> getProductlist() {
        return dao.getProductlist();
    }
    public ProductDAO getDAO(){
        return dao;
    }

    private void setDAO(ProductDAO dao) {
        this.dao = dao;
    }

    public boolean addProduct(Product p) {
        return dao.addProduct(p);
    }

    public List<Product> findProducts(int id, String name, String descr,
            double lownormalprice, double highnormalprice,
            double lowmembershipprice, double highmembershiprice,
            Timestamp pdate, int categoryid) {
        // TODO Auto-generated method stub
        return null;
    }

    public void deleteProductByCategoryId(int categoryid) {
        // TODO Auto-generated method stub

    }

    public void deleteProductById(int id) {
        // TODO Auto-generated method stub

    }

    public boolean updateProduct(Product p) {
        // TODO Auto-generated method stub
        return false;
    }
}

这样可以大大的提高代码的灵活性。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值