简体   繁体   English

这是什么错误? (Java数据库应用程序)

[英]What is this error? (Java database application)

I have the following code and I'm trying to return data from my database (itemName, itemId). 我有以下代码,并且我试图从数据库(itemName,itemId)返回数据。 Yet it gives me the following error: 但这给了我以下错误:

Items() in Items cannot be applied to:
Expected         Actual
Parameters:      Arguments:

pool: com.sun.tools.javac.jvm.Pool     itemName (java.lang.String)
code: com.sun.tools.javac.jvm.Code itemId    (java.lang.String)
symtab: com.sun.tools.javac.code.Symtab     quantity (java.lang.String)
types: com.sun.tools.javac.code.Types     cost (java.lang.String)

Here is my code: 这是我的代码:

public List<Items> getItemList() throws SQLException{
    try {
        Statement statement = connection.createStatement();
        ResultSet results = statement.executeQuery("SELECT * FROM Items");
        {

            List<Items> itemsList = new ArrayList<>();

            while (results.next()) {
                String itemName = String.valueOf(results.getString("item_name"));
                String itemId = String.valueOf(results.getString("item_id"));
                Items items = new Items(itemName, itemId); // where the error is
                itemsList.add(items);
            }

            return itemsList;
        }

    } catch(Exception e){
        e.printStackTrace();
    }

    return null;
}

Is it because the types are incompatible (object v. string)? 是因为类型不兼容(对象与字符串)? If so, String.valueOf... incorrect? 如果是这样,String.valueOf ...不正确?

EDIT 编辑

public class Items {

// Constructor
public Items(String itemName, String itemId){
    setItemName(itemName);
    setItemId(itemId);
}

// itemName
private final StringProperty itemName = new SimpleStringProperty(this,"itemName");

public StringProperty itemNameProperty(){
    return itemName;
}

public final String getItemName(){
    return itemNameProperty().get();
}

public final void setItemName(String itemName){
    itemNameProperty().set(itemName);
}

// itemId
private final StringProperty itemId = new SimpleStringProperty(this,"itemId");

public StringProperty itemIdProperty(){
    return itemId;
}

public final String getItemId(){
    return itemIdProperty().get();
}

public final void setItemId(String itemId){
    itemIdProperty().set(itemId);
}


}

That looks like an issue that is caused by importing an Items class from the wrong package. 这看起来像是由于从错误的包中导入Items类引起的问题。 If you have an import statement of the form 如果您有以下形式的进口声明

import some.package.name.Items ;

near the top of the class containing your first block of code, then remove that line. 在包含您的第一段代码的类顶部附近,然后删除该行。

If the class containing the first block of code is in a different package to your Items class, then you need to add in a line 如果包含第一段代码的类与Items类位于不同的包中,则需要添加一行

import the.correct.package.Items ;

in place of the incorrect import statement. 代替错误的import语句。 Replace the.correct.package with the name of the package containing your Items class (in Items.java , look at the first line of code, which is a package statement defining which package contains Items ). the.correct.package替换为包含Items类的包的名称(在Items.java ,查看代码的第一行,这是定义哪个包包含Itemspackage语句)。

For some background information, I recommend Meaning of the import statement in a Java file 有关一些背景信息,我建议在Java文件中使用import语句的含义

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM