简体   繁体   English

将结果集值传递给另一个类

[英]Passing result set values into another class

I have 2 java classes, one being color.java and the other being size.java.我有 2 个 java 类,一个是 color.java,另一个是 size.java。 The color class has my SQL statement pulling from multiple tables and my result set.颜色类有我的 SQL 语句从多个表和我的结果集中提取。 What I'm trying to do is pass in the itemno and description from color into size.我想要做的是将 itemno 和 description 从颜色传递到大小。 Once I pass those values into size.java it will do additional logic and then come back to color.java to finish.一旦我将这些值传递给 size.java,它就会执行额外的逻辑,然后返回 color.java 来完成。

I've tried using something like this:我试过使用这样的东西:

public void getValues(String color.rs.itemno, String color.rs.description){
return itemno;
return description;
}

I'm new to java so I may not be on the right track, but looking at other examples I think it goes something like the above.我是 Java 新手,所以我可能不在正确的轨道上,但查看其他示例,我认为它与上述类似。

you need to collect all values from a result set in a container, array or arraylist or other, loop through them and then pass whatever you need to your function.您需要从容器、数组或数组列表或其他的结果集中收集所有值,遍历它们,然后将您需要的任何内容传递给您的函数。 Seen here 在这里看到

(String color.rs.itemno, String color.rs.description)

can should be changed to可以改为

(String itemno, String description)

Okay so there are a couple of things wrong with the above code:好的,上面的代码有一些错误:

  1. Currently, you have what is known as a mutator method.目前,您拥有所谓的mutator方法。 This is a method which does not return a value.这是一种不返回值的方法。 Instead this is used to perform operations within an object, usually passing several parameters through it.相反,它用于在对象内执行操作,通常通过它传递几个参数。 In order to return a value, you will make use of an accessor method.为了返回一个值,您将使用访问器方法。 For this, you need to state the data type you wish to return.为此,您需要说明要返回的数据类型。
  2. Next, you have named your variables incorrectly.接下来,您错误地命名了变量。 You may wish to look at this document for the naming conversions here: https://www.javatpoint.com/java-naming-conventions What you have created now, are temporary variables only accessible within that method.您可能希望在此处查看此文档以了解命名转换: https://www.javatpoint.com/java-naming-conventions您现在创建的是只能在该方法中访问的临时变量。
  3. Lastly, you cannot return more than one variable at once.最后,您不能一次返回多个变量。 The nearest path will be taken and then the code stops running from there.将采用最近的路径,然后代码停止从那里运行。 So in your case, you would only be returning itemno and then the code will stop.因此,在您的情况下,您只会返回 itemno,然后代码将停止。

Here is an example how the code should look like keeping the above in mind:下面是一个示例代码应该如何记住上述内容:

String itemDescription;
int itemNumber;

public void setItemNo(int in)
{
    itemNumber = in;
}

public void setItemDescription(String id)
{
    itemDescription = id;
}

public int getItemNo()
{
    return itemNumber;
}

public String getItemDescription()
{
    return itemDescription;
}

I would however recommend making use of a constructor to set these values in the beginning:但是,我建议在开始时使用构造函数来设置这些值:

public ClassName(int in, String id)
{
    itemNumber = in;
    itemDescription = id;
}

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

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