Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ms-access/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java中swing combobox中的数据库绑定_Java_Mysql_Swing_Combobox - Fatal编程技术网

java中swing combobox中的数据库绑定

java中swing combobox中的数据库绑定,java,mysql,swing,combobox,Java,Mysql,Swing,Combobox,我想在组合框中以升序显示不同的年份。但是combox中的输出看起来是重复的,重复的意思是它显示了数据库列的所有元素,但我只想显示列的不同数据。我做错了什么??我以前在php中只做过简单的数据库查询。我知道这很简单,但我想我并没有按照正确的顺序解决这个问题 我的代码如下 /* * To change this license header, choose License Headers in Project Properties. * To change this template file,

我想在组合框中以升序显示不同的年份。但是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);
    }

}
我想在组合框中以升序显示不同的年份。但是combobox中的输出看起来是重复的,重复的意思是它显示了数据库列的所有元素,但我只想显示列的不同数据

你说这就是你用来完成这个任务的查询

@NamedQuery(name = "Production.findByYearOfProduction", 
            query = "SELECT DISTINCT p FROM Production p 
                     WHERE p.yearOfProduction = :yearOfProduction
                     ORDER Bp.yearOfProduction ASC" )})
这个查询的问题是,它用于查找生产年份的生产实体列表。假设您使用1990年的查询。这意味着1990年生产的任何生产实体都必须是结果集的一部分。因此,你将看到的唯一生产年份是1990年

您需要的只是单个列中的不同值,其中不一定涉及实际生产实体。因此,您的查询应该更像

SELECT DISTINCT p.yearOfProduction 
FROM Production p
ORDER BY p.yearOfProduction ASC
返回值应该是仅包含不同年份的列表,并且与生产实体没有关联。您可能需要对代码进行重构,调用此查询将返回一个
列表
。这是您要用于组合框的列表,而不是
列表。您应该在哪里进行重构?我不知道,因为您没有提供调用此查询的代码

注意:您应该创建一个单独的@NamedQuery,以及您当前拥有的@NamedQuery,我认为您可能希望保留一个不同的组合框,在第一个组合框中选择一年后,第二个组合框将填充该年的所有生产实体。还要注意,您的toString只列出了生产年份。因此,如果您希望在第二个组合框中使用生产实体的其他表示形式,您也应该对其进行更改


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

显示构建JComboBox的代码。我怀疑您在重复向现有列表或模型添加对象,而不是创建新列表/模型或在添加之前清除现有列表/模型。@VGR look I am binding combobox。。。我右键单击组合框并直接将其与数据库绑定,因此自动创建了此代码。以下查询是否可以按升序排列不同的年份@NamedQuery(name=“Production.findByYearOfProduction”,query=“选择与生产p不同的p,其中p.yearOfProduction=:yearOfProduction ORDER BY p.yearOfProduction ASC”))另请参见此相关内容。@peeskillet否查询不起作用。它仍然是重复的。1990年出现了三次,其他年份也出现了三次。问题是什么problem@enjal你能告诉我你是如何调用这个查询的吗。更新你的帖子。在mysql shell中尝试查询,看看结果是什么。它应该会起作用。如果是这样,那么你知道你的问题出在哪里了