如何在 setString 内调用一个方法

huangapple go评论76阅读模式
英文:

How do I call a method within a setString

问题

代码部分不进行翻译,仅返回翻译后的内容:

我正在尝试使用setString从另一个类中调用一个方法,但我收到一个错误消息,该消息表示<variableName>无法解析为变量。

在我的Tag类中,我有:

import java.util.List;
import models.Book;

public class Tag {

    private String isbn_13;
    private String tag_name;

    public Tag(String isbn, String tagName) {
        this.isbn_13 = isbn;
        this.tag_name = tagName;
    }

    public Tag() {
        this.isbn_13 = null;
        this.tag_name = null;
    }

    public String getIsbn13() {
        return isbn_13;
    }

    public void setIsbn13(String isbn) {
        this.isbn_13 = isbn;
    }

    public String getTag() {
        return tag_name;
    }

    public void setTag(String tagName) {
        this.tag_name = tagName;
    }

    public void add(List<Tag> book_tags) {
        // 待办事项:自动生成的方法存根
    }
}

现在,在我的TagImpl类中,相关方法是:

public boolean addTag(Tag tag) {
    try {
        connection = DAOUtilities.getConnection();
        String sql = "INSERT INTO Book_tags VALUES (?, ?)"; 
        stmt = connection.prepareStatement(sql);
        
        stmt.setString(1, tag.getIsbn13()); // 错误在这里:"isbn 无法解析为变量"
        stmt.setString(2, tag.getTag());    // 错误在这里:"tagName 无法解析为变量"
        
        if (stmt.executeUpdate() != 0)
            return true;
        else
            return false;
        
    } catch (SQLException e) {
        e.printStackTrace();
        return false;
    } finally {
        closeResources();
    }
}

我已经查看了所有我能想到的文档和教程,它们都看起来是正确的。唯一的区别是其中一些显示了空括号,即stmt.setString(1, tag.getIsbn13());,但这只会产生另一个错误:"在类型 Tag 中的 setIsbn13(String) 方法不适用于参数()"。同时,将(isbn)更改为(isbn_13)或(tagName)更改为(tag_name)也不会产生任何区别 —— 它仍然"无法解析为变量"。

英文:

I am trying to use a setString to call a method from another class, but I get an error message that says that <variableName> cannot be resolved to a variable.

In my Tag class, I have:

import java.util.List;
import models.Book;


public class Tag {

	private String isbn_13;
	private String tag_name;
	
	public Tag(String isbn, String tagName) {
		this.isbn_13 = isbn;
		this.tag_name = tagName;
	}
	
	public Tag() {
		this.isbn_13 = null;
		this.tag_name = null;
	}
	
	public String getIsbn13() {
		return isbn_13;
	}

	public void setIsbn13(String isbn) {
		this.isbn_13 = isbn;
	}
	
	public String getTag() {
		return tag_name;
	}
	
	public void setTag(String tagName) {
		this.tag_name = tagName;
	}
	
	public void add(List&lt;Tag&gt; book_tags) {
		// TODO Auto-generated method stub
		
	}

}

Now, in my TagImpl class, the relevant method is

public boolean addTag(Tag tag) {
		try {
			connection = DAOUtilities.getConnection();
			String sql = &quot;INSERT INTO Book_tags VALUES (?, ?)&quot;; 
			stmt = connection.prepareStatement(sql);
			
			stmt.setString(1, tag.setIsbn13(isbn)); // Error here &quot;isbn cannot be resolved to a variable&quot;
			stmt.setString(2, tag.setTag(tagName)); // Error here &quot;tagName cannot be resolved 
                                                    // to a variable&quot;
			
			if (stmt.executeUpdate() != 0)
				return true;
			else
				return false;
			
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		} finally {
			closeResources();
		}
	}

I have looked at all the documentation I can think of, and the tutorials I can find, and all of them look like this is correct. The only difference is that some of them show empty parentheses, i.e. stmt.setString(1, tag.setIsbn13()); but that just gives a different error, "The method setIsbn13(String) in the type Tag is not applicable for the arguments ()" Also, it makes no difference if I change (isbn) to (isbn_13) or (tagName) to (tag_name) -- it still "cannot be resolved to a variable.

答案1

得分: 1

你必须从标签中获取 isbn_13tag_name,并将它们设置到 stmt 中。

将以下内容替换:

stmt.setString(1, tag.setIsbn13(isbn)); 
stmt.setString(2, tag.setTag(tagName));

用以下内容替代:

stmt.setString(1, tag.getIsbn13()); 
stmt.setString(2, tag.getTag());
英文:

You have to get the isbn_13 and tag_name from tag and set into stmt.

Replace

stmt.setString(1, tag.setIsbn13(isbn)); 
stmt.setString(2, tag.setTag(tagName));

with

stmt.setString(1, tag.getIsbn13()); 
stmt.setString(2, tag.getTag());

答案2

得分: 1

我不确定您在setIsbn13调用中尝试做什么。我认为您只需使用tag.getIsbn13()。您是想从标签中获取Isbn13值并将其发送到stmt.setString()函数吗?我假设您的方法已传递了已填充值的标签,并且您希望在预备语句中使用这些值。

您手头没有名为isbn的东西,这就是问题所在。但即使您有,仍会出问题,因为setIsbn13方法返回void,然后您尝试将结果传递给stmt.setString。这也将是一个错误。

我假设您尝试做这个操作。您可以内联调用,但我创建了一个新的局部变量,以使其更清晰。

public boolean addTag(Tag tag) {
    try {
        connection = DAOUtilities.getConnection();
        String sql = "INSERT INTO Book_tags VALUES (?, ?)"; 
        stmt = connection.prepareStatement(sql);

        String isbn = tag.getIsbn13();
        String tagName = tag.getName();  // 您将这个命名为getTag,但请更改它。
        
        stmt.setString(1, isbn); 
        stmt.setString(2, tagName);
        
        if (stmt.executeUpdate() != 0)
            return true;
        else
            return false;
        
    } catch (SQLException e) {
        e.printStackTrace();
        return false;
    } finally {
        closeResources();
    }
}
英文:

I'm not sure what you are trying to do with the setIsbn13 call, there. I think that you just want tag.getIsbn13(). Are you looking to get the Isbn13 value out of the tag and send it to the stmt.setString() function? Presumably, your method was passed the tag, which has already been filled with values, and you want to use those values in your preparedStatement.

You don't have anything named isbn in your hand, which is the problem. But even if you did, you'd still be in trouble, because setIsbn13 method returns a void, which you then are trying to pass the result to stmt.setString. That will be an error, too.

I assume you're trying to do this. You could inline the call, but I made a new local variable to make it more clear what's happening.

public boolean addTag(Tag tag) {
    try {
        connection = DAOUtilities.getConnection();
        String sql = &quot;INSERT INTO Book_tags VALUES (?, ?)&quot;; 
        stmt = connection.prepareStatement(sql);

        String isbn = tag.getIsbn13();
        String tagName = tag.getName();  // You named this getTag, but please change it.
        
        stmt.setString(1, isbn); 
        stmt.setString(2, tagName);
        
        if (stmt.executeUpdate() != 0)
            return true;
        else
            return false;
        
    } catch (SQLException e) {
        e.printStackTrace();
        return false;
    } finally {
        closeResources();
    }
}

huangapple
  • 本文由 发表于 2020年10月17日 04:11:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/64395796.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定