类中的Java方法不能应用于给定的类型

时间:2017-01-13 14:04:29

标签: java netbeans

我正在尝试将类searchitem中名为Query的方法调用到名为Search的jbutton,它将搜索数据库中表项的所有详细信息并将其显示在jtextfields中。下面是Query类中的searchitem方法。

public ResultSet searchitem() {
    try {
        String sql = "select * from tblitem";
        stmt = conn.connect.createStatement();
        rs = stmt.executeQuery(sql);
        return rs;
    } catch (SQLException ex) {
        System.out.println("SQL Exception in Seach Book "+ex.getMessage());
        JOptionPane.showMessageDialog(null, "Error in Retriving Book Information", "Error", JOptionPane.ERROR_MESSAGE);
        Logger.getLogger(Query.class.getName()).log(Level.SEVERE, null, ex);
        return null;
    }
}

这是jbutton中的代码。

try {

        String id, nme,cos,pr,available,sid,search;

       search= stf1.getText();

        if(search.length()!=5 || !search.toLowerCase().startsWith("i")){
            JOptionPane.showMessageDialog(null, "Incorrect Item ID \nItem ID should start with 'i' and 5 characters should be used ", "Error", WIDTH);
        }


        else{
            Query query = new Query();
            rs = query. searchitem(search);
            rs.next();

            id = rs.getString(1);
            nme= rs.getString(2);
            cos = rs.getString(3);
            pr = rs.getString(4);
            available = rs.getString(5);
            sid = rs.getString(6);

           boolean availablity = Boolean.valueOf(available);

            etf1.setText(id);
            etf2.setText(nme);
            etf3.setText(cos);
            etf4.setText(pr);
            ecb1.setSelected(availablity);
            etf5.setText(sid);
        }

          } catch (SQLException ex) {
        JOptionPane.showMessageDialog(null, "Item not Available. \nEnter an existing Item ID", "Unavailable", JOptionPane.ERROR_MESSAGE);
        System.out.println("SQL Exception in retriving Item details");
        Logger.getLogger(Item.class.getName()).log(Level.SEVERE, null, ex);
    }
}                                        

但我在第rs = query. searchitem(search);行说

时收到错误 类查询中的方法searchitem不能应用于给定的类型;
  要求:没有参数
  发现:字符串
  原因:实际和正式的参数列表长度不同

1 个答案:

答案 0 :(得分:0)

您的方法segnature没有参数:public ResultSet searchitem() {

但你用一个参数rs = query. searchitem(search);来称呼它。那不合适。

因此,在调用函数时删除参数,或者实现一个期望一个参数类型正确的函数