在DTO和DAO之间传递数据

时间:2016-04-19 10:16:02

标签: java oop

我想将数据从AdminBussiness类传递到DTO,然后传递给DAO。

问题是,我的数据没有从AdminBussiness类一直移动到DAO。

package com.diariofacil.business;

import com.diariofacil.pojo.Product;
import com.diariofacil.service.DiarioFacilException;
import com.diariofacil.transferobject.ProductTO;

/**
 *
 * @author asdrubal
 */
public class AdminBussiness {

public String product, description, code;
public double price;
public int productQuantity;

Product productClass = new Product();

public AdminBussiness() {
}

public AdminBussiness(String product, double price, int productQuantity, String description, String code) {
    this.product = product;
    this.price = price;
    this.productQuantity = productQuantity;
    this.description = description;
    this.code = code;
}

  /*gets and sets*/

public void addProduct(ProductTO productTO) throws DiarioFacilException{
    productClass.setNombre(this.getProduct());
    productClass.setPrice(this.getPrice());
    productClass.setMinimunStock(this.getProductQuantity());
    productClass.setDescription(this.getDescription());
    productClass.setCode(this.getCode());
    productTO.insert(productClass);
}

public void updateProduct(ProductTO productTO) throws DiarioFacilException, Exception {
    productClass.setNombre(this.getProduct());
    productClass.setPrice(this.getPrice());
    productClass.setMinimunStock(this.getProductQuantity());
    productClass.setDescription(this.getDescription());
    productClass.setCode(this.getCode());
    productTO.update(productClass);
}

}

addProduct方法未添加。

    package com.diariofacil.pojo;

import com.diariofacil.interfaces.Soldable;
import java.io.Serializable;

public class Product extends Item implements Serializable, Soldable {
    Provider provider;
    Category category;
    int minimunStock,id;

    public Product(int id,Provider provider, Category category, int minimunStock, String nombre, String description, String code, double price) {
    super(nombre, description, code, price);
    this.provider = provider;
    this.category = category;
    this.minimunStock = minimunStock;
}

public Product(String nombre, String description, String code, double price) {
    super(nombre, description, code, price);
}




public Product() {
}

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}   

public Provider getProvider() {
    return provider;
}

public void setProvider(Provider provider) {
    this.provider = provider;
}

public Category getCategory() {
    return category;
}

public void setCategory(Category category) {
    this.category = category;
}

public int getMinimunStock() {
    return minimunStock;
}

public void setMinimunStock(int minimunStock) {
    this.minimunStock = minimunStock;
}



}
    package com.diariofacil.transferobject;

import com.diariofacil.pojo.*;
import com.diariofacil.service.DiarioFacilException;
import com.diariofacil.service.*;
import java.io.Serializable;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

public class ProductTO implements Serializable{
     private ProductDAO dao = new ProductDAO();

public ProductTO() {
}

public void insert(Product product) throws DiarioFacilException {
    dao.insert(product);
}    
public void delete(Product product) throws DiarioFacilException{
        dao.delete(product);
}
public void update(Product product) throws DiarioFacilException, Exception{
    dao.update(product);
}


public List<Product> search() throws DiarioFacilException{
        return dao.SearchProduct();
    }
}

和DAO

package com.diariofacil.service;

import com.diariofacil.pojo.*;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ProductDAO extends ServiceBase {

private static final String INSERT = "INSERT INTO `product`(`id`,`price`,`description`,"
                                    + "`stock`,`category`,`seller` VALUES (?,?,?,?,?,?,?)";

private static final String DELETE ="DELETE FROM `diariofacil`.`product`\n" +
                                    "WHERE ID VALUES (?);";
 private static final String SELECT ="SELECT `product`.`id`,`product`.`name`"+
                                     "FROM `diariofacil`.`product`;";
 private static final String UPDATE = "UPDATE product SET id = ?, price = ?, description = ?, category = ?, seller = ?, minimum_stock "
                                      + "WHERE id = ?";


public void insert(Product product) throws DiarioFacilException {
    this.connect();
    try {
        PreparedStatement pstmt = this.getConexion().prepareStatement(INSERT);

        pstmt.setInt(1, 1);
        pstmt.setDouble(2, product.getPrice());
        pstmt.setString(3, product.getDescription());
        pstmt.setInt(4, product.getMinimunStock());
        pstmt.setObject(5, product.getCategory());
        pstmt.setObject(6, product.getProvider());


        pstmt.execute();

    } catch (SQLException ex) {
        ex.printStackTrace();
        throw new DiarioFacilException("No se puedo insertar el registro.");
    } finally {
        this.disconect();
    }
}

public void update(Product product) throws Exception{
    this.connect();

    try {
        PreparedStatement pstmt = this.getConexion().prepareStatement(UPDATE);

        pstmt.setInt(1, product.getId());
        pstmt.setDouble(2, product.getPrice());
        pstmt.setString(3, product.getDescription());
        pstmt.setInt(4, product.getMinimunStock());

    } catch (SQLException ex) {
        ex.printStackTrace();
        throw new Exception("No se pudo actualizar el registro");
    } finally {
        this.disconect();
    }
}

public void delete(Product product) throws DiarioFacilException {
    this.connect();

    try {
        PreparedStatement pstmt = this.getConexion().prepareStatement(DELETE);

        pstmt.setInt(1, product.getId());
        pstmt.executeUpdate();

    } catch (SQLException ex) {
        ex.printStackTrace();
        throw new DiarioFacilException("No se puedo eliminar el registro.");
    } finally {
        this.disconect();
    }

}

如何在第一个类的addProduct方法中将数据从productTo移动到productClass?

1 个答案:

答案 0 :(得分:0)

或许在ProductDAO中插入查询不太正确?

不应该包含右括号&#34;)&#34;在VALUES之前?