【问题标题】:Binding from database in swing combobox in java在java中的swing组合框中从数据库绑定
【发布时间】:2014-08-26 07:07:51
【问题描述】:

我想在组合框中按升序显示不同的年份。但是combox中的输出似乎是重复的,重复的,因为它显示了数据库列的所有元素,但我只想显示列的不同数据。我究竟做错了什么??我之前在 php 中简单地完成了简单的数据库查询。我知道这很简单,但我猜我并没有按正确的顺序处理查询。

我的代码如下。

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package my_ui;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.io.Serializable;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.persistence.Transient;

/**
 *
 * @author enjal
 */
@Entity
@Table(name = "production", catalog = "data2", schema = "")
@NamedQueries({
    @NamedQuery(name = "Production.findAll", query = "SELECT p FROM Production p"),
    @NamedQuery(name = "Production.findByProductionId", query = "SELECT p FROM Production p WHERE p.productionId = :productionId"),
    @NamedQuery(name = "Production.findByCropId", query = "SELECT p FROM Production p WHERE p.cropId = :cropId"),
    @NamedQuery(name = "Production.findByLocationId", query = "SELECT p FROM Production p WHERE p.locationId = :locationId"),
    @NamedQuery(name = "Production.findByArea", query = "SELECT p FROM Production p WHERE p.area = :area"),
    @NamedQuery(name = "Production.findByProductionAmount", query = "SELECT p FROM Production p WHERE p.productionAmount = :productionAmount"),
    @NamedQuery(name = "Production.findByYieldAmount", query = "SELECT p FROM Production p WHERE p.yieldAmount = :yieldAmount"),
    @NamedQuery(name = "Production.findByYearOfProduction", query = "SELECT DISTINCT p FROM Production p WHERE p.yearOfProduction = :yearOfProduction ORDER BY p.yearOfProduction ASC" )})
//SELECT DISTINCT year_of_production FROM `production` ORDER BY year_of_production ASC
public class Production implements Serializable {
    @Transient
    private PropertyChangeSupport changeSupport = new PropertyChangeSupport(this);
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "production_id")
    private Integer productionId;
    @Basic(optional = false)
    @Column(name = "crop_id")
    private int cropId;
    @Basic(optional = false)
    @Column(name = "location_id")
    private String locationId;
    @Basic(optional = false)
    @Column(name = "area")
    private double area;
    @Basic(optional = false)
    @Column(name = "production_amount")
    private String productionAmount;
    @Basic(optional = false)
    @Column(name = "yield_amount")
    private double yieldAmount;
    @Basic(optional = false)
    @Column(name = "year_of_production")
    private String yearOfProduction;

    public Production() {
    }

    public Production(Integer productionId) {
        this.productionId = productionId;
    }

    public Production(Integer productionId, int cropId, String locationId, double area, String productionAmount, double yieldAmount, String yearOfProduction) {
        this.productionId = productionId;
        this.cropId = cropId;
        this.locationId = locationId;
        this.area = area;
        this.productionAmount = productionAmount;
        this.yieldAmount = yieldAmount;
        this.yearOfProduction = yearOfProduction;
    }

    public Integer getProductionId() {
        return productionId;
    }

    public void setProductionId(Integer productionId) {
        Integer oldProductionId = this.productionId;
        this.productionId = productionId;
        changeSupport.firePropertyChange("productionId", oldProductionId, productionId);
    }

    public int getCropId() {
        return cropId;
    }

    public void setCropId(int cropId) {
        int oldCropId = this.cropId;
        this.cropId = cropId;
        changeSupport.firePropertyChange("cropId", oldCropId, cropId);
    }

    public String getLocationId() {
        return locationId;
    }

    public void setLocationId(String locationId) {
        String oldLocationId = this.locationId;
        this.locationId = locationId;
        changeSupport.firePropertyChange("locationId", oldLocationId, locationId);
    }

    public double getArea() {
        return area;
    }

    public void setArea(double area) {
        double oldArea = this.area;
        this.area = area;
        changeSupport.firePropertyChange("area", oldArea, area);
    }

    public String getProductionAmount() {
        return productionAmount;
    }

    public void setProductionAmount(String productionAmount) {
        String oldProductionAmount = this.productionAmount;
        this.productionAmount = productionAmount;
        changeSupport.firePropertyChange("productionAmount", oldProductionAmount, productionAmount);
    }

    public double getYieldAmount() {
        return yieldAmount;
    }

    public void setYieldAmount(double yieldAmount) {
        double oldYieldAmount = this.yieldAmount;
        this.yieldAmount = yieldAmount;
        changeSupport.firePropertyChange("yieldAmount", oldYieldAmount, yieldAmount);
    }

    public String getYearOfProduction() {
        return yearOfProduction;
    }

    public void setYearOfProduction(String yearOfProduction) {
        String oldYearOfProduction = this.yearOfProduction;
        this.yearOfProduction = yearOfProduction;
        changeSupport.firePropertyChange("yearOfProduction", oldYearOfProduction, yearOfProduction);
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (productionId != null ? productionId.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Production)) {
            return false;
        }
        Production other = (Production) object;
        if ((this.productionId == null && other.productionId != null) || (this.productionId != null && !this.productionId.equals(other.productionId))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "my_ui.Production[ productionId=" + yearOfProduction + " ]";
    }

    public void addPropertyChangeListener(PropertyChangeListener listener) {
        changeSupport.addPropertyChangeListener(listener);
    }

    public void removePropertyChangeListener(PropertyChangeListener listener) {
        changeSupport.removePropertyChangeListener(listener);
    }

}

【问题讨论】:

  • 显示构建 JComboBox 的代码。我怀疑您反复将对象添加到现有列表或模型中,而不是创建新列表/模型或在添加之前清除现有列表/模型。
  • @VGR 看我正在绑定组合框...我右键单击组合框并将其与数据库直接绑定。因此,此代码是自动创建的。以下查询是否适合按升序排列不同年份。@NamedQuery(name = "Production.findByYearOfProduction", query = "SELECT DISTINCT p FROM Production p WHERE p.yearOfProduction = :yearOfProduction ORDER BY p.yearOfProduction ASC" )})

标签: java mysql swing combobox


【解决方案1】:

我想在组合框中按升序显示不同的年份。但是组合框中的输出似乎是重复的,重复的,因为它显示了数据库列的所有元素,但我只想显示列的不同数据

你说这是你用来完成的查询

@NamedQuery(name = "Production.findByYearOfProduction", 
            query = "SELECT DISTINCT p FROM Production p 
                     WHERE p.yearOfProduction = :yearOfProduction
                     ORDER Bp.yearOfProduction ASC" )})

这个查询的问题是它被用来按 yearOfProduction 查找生产实体列表。因此,假设您将此查询与 1990 年一起使用。这意味着 1990 年生成的任何生产实体都是结果集的一部分。因此,您将看到的唯一生产年份是 1990 年。

您想要的只是单个列中的不同值,而实际的生产实体不一定要关注。所以你的查询应该更像

SELECT DISTINCT p.yearOfProduction 
FROM Production p
ORDER BY p.yearOfProduction ASC

返回值应该是一个仅包含不同年份的列表,并且与 Production 实体无关。您可能想要对代码进行一些重构,调用此查询将返回List<String>。这就是您要用于组合框的列表,而不是List<Production>。你应该在哪里进行重构?我不知道,因为您没有提供调用此查询的代码。

注意:所以你应该做的是创建一个单独的@NamedQuery,而你目前拥有的那个,我想你可能想保留一个不同的组合框,一年后从第一个组合框中选择,第二个是填充当年的所有生产实体。另请注意,您的 toString 仅列出了生产年份。因此,如果您想在第二个组合框中显示生产实体的其他表示形式,您也应该更改它。

还请记住,当您在 Netbeans 中自动创建实体时,它将为实体的每个属性创建一个 findAll 查询和一个查询 findByXxx 查询。而已。所有查询都旨在返回使用一个 that 属性作为查找匹配结果的参数的生产实体列表。您想要的任何其他返回类型(在本例中为字符串列表)或不同的参数查询,您需要创建自定义查询

【讨论】:

  • 另见相关examplesuggestions
  • @peeskillet 否,查询不起作用。它仍然重复。 1990 年出现了 3 次,其他年份也出现了 3 次。有什么问题
  • @enjal 你能说明你是如何调用这个查询的吗?更新你的帖子。在 mysql shell 中尝试查询,看看结果如何。它应该工作。如果是这样,那么您知道您的问题出在其他地方
最近更新 更多