本文主要是介绍关于接口在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 stubreturn 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 stubreturn 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 stubreturn 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 stubreturn 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 stubreturn false;}
}
这样可以大大的提高代码的灵活性。
这篇关于关于接口在J2EE编程中的小运用及一些体会的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!