首页 > 解决方案 > 我的构造函数定义不明确?

问题描述

我有一个名为 Album 的表,它有 4 列,CodeA 是 char 类型的主键。

然后我的第二个表是 Collabo,其中包含 1 个名为 CodeA 的列,它始终是 char 类型的外键。

这是 我尝试在我的 Jframe Collabo 中显示 CodeA 的预览,但我的表 DaoCollabo 中有错误消息。

没有找到适合 Album(String) 的构造函数构造函数 Album.Album() 不适用(实际参数列表和正式参数列表长度不同)构造函数 Album.Album(String,String,Chanteur,Date) 不适用(实际参数列表和正式参数列表长度不同)

你觉得我的班级专辑和合作怎么样?

班级相册

public class Album {
    private String codeA;
    private String titreA;
    private Chanteur chantAlb;
    private Date dateApp; //


    public Album() {
    }

    public Album(String codeA, String titreA, Chanteur chantAlb, Date dateApp) {
        this.codeA = codeA;
        this.titreA = titreA;
        this.chantAlb = chantAlb;
        this.dateApp = dateApp;
    }

    public String getCodeA() {
        return codeA;
    }

    public void setCodeA(String codeA) {
        this.codeA = codeA;
    }

    public String getTitreA() {
        return titreA;
    }

    public void setTitreA(String titreA) {
        this.titreA = titreA;
    }

    public Chanteur getChantAlb() {
        return chantAlb;
    }

    public void setChantAlb(Chanteur chantAlb) {
        this.chantAlb = chantAlb;
    }

    public Date getDateApp() {
        return dateApp;
    }

    public void setDateApp(Date dateApp) {
        this.dateApp = dateApp;
    }

   public String getDateAppBE() {
        String tmp;

        if (this.dateApp == null)
          tmp = "";
        else
          {
          SimpleDateFormat dateParser = new SimpleDateFormat("dd/MM/yyyy");
          tmp = dateParser.format(this.dateApp);
          }
        return tmp;
    }

    public void setDateAppBE(String dateApp) {
        SimpleDateFormat dateParser = new SimpleDateFormat("dd/MM/yyyy");
        try {
            this.dateApp = dateParser.parse(dateApp);
        } catch (ParseException ex) {
            Logger.getLogger(Album.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public String getDateAppSQL() {
        String tmp;

        if (this.dateApp == null)
          tmp = "";
        else
          {
          SimpleDateFormat dateParser = new SimpleDateFormat("yyyy-MM-dd");
          tmp = "'" + dateParser.format(this.dateApp) + "'";
          }
        return tmp;
    }

    public String getDateAppUS() {
        String tmp;

        if (this.dateApp == null)
          tmp = "";
        else
          {
          tmp = this.dateApp.toString();
          }
        return tmp;
    }  

我的课堂协作

public class Collabo {
    private Album appAlb;

    public Collabo() {
    }
    public Collabo(Album appAlb) {
        this.appAlb = appAlb;
    }
    public Album getAppAlb() {
        return appAlb;
    }
    public void setAppAlb(Album appAlb) {
        this.appAlb = appAlb;
    }

DAOCollabo

public ArrayList <Collabo> selectCollabos()
    {
        ArrayList <Collabo> myList = new ArrayList();


        String req = "Select A.CodeA from album A, collabo C where A.CodeA = C.CodeA order by 1 ";
        ResultSet resu = ConnexionMySQL.getInstance().selectQuery (req);
        try {
            while (resu.next())
            {  
                //creation de l'objet Collabo
                myList.add (new Collabo(resu.getString(1), 
                             new Album (resu.getString(2))));
             }
        }
        catch (SQLException e)
        {
            System.out.println(e.toString());
            System.exit(-1);
        }
        return myList;
    }

标签: java

解决方案


new Album (resu.getString(2))

需要一个带有单个String参数的构造函数:

public Album(String codeA) {
    this.codeA = codeA;
}

推荐阅读