2014-07-09 50 views
2

我已經使用netbeans中的GUI將我的組合框與MySQL數據庫綁定在一起。我整年都在組合框中。但是我無法獲得組合框中獨特的年份。在Java中從MySQL中獲取不同的行

The query that displays all year is : 
query = "SELECT p FROM Production p WHERE p.yearOfProduction = :yearOfProduction"; 

我修改了查詢,如下所示,以顯示不同的yearOfProduction。但是查詢不會更改組合框中的列表。

query = "SELECT DISTINCT p FROM Production p WHERE p.yearOfProduction = :yearOfProduction ORDER BY yearOfProduction"; 

我該怎麼辦?這種類型的查詢對我來說是新的。

的自動生成的Java代碼是:

/* 
    * 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 p FROM Production p WHERE p.yearOfProduction = :yearOfProduction")}) 
//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 "" + yearOfProduction + ""; 
    } 

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

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

} 
+1

您所查詢的是尋找不同的生產實例,而不是不同的生產年份。 – JamesB

+0

JamesB我是這種類型的查詢的新手..你可以寫我單行代碼來做到這一點......這將是非常有幫助的 – enjal

回答

1

獲取不同的一年

select distinct (p.yearOfProduction) FROM Production p 

查詢應不需要任何一年參數。如果你過了一年,你只會得到一年。

+0

+1還應該指出,這個查詢的返回類型將有所不同。日期的集合,而不是生產。 – JamesB

+0

@JamesB我在生產表中有生產年份欄。我想獲得明顯的歲月。我應該怎麼做。 – enjal

+0

@ vels4j不,它不工作..錯誤發生 – enjal

0

通過yearOfProduction 選擇生產對訂單不同p.yearOfProduction **我的意見是嘗試使用GROUP BY然後應用獨特的鍵盤**

+0

我該怎麼做?可能你可以幫忙 – enjal