Java 在另一个类中使用一个类的结果?

Java 在另一个类中使用一个类的结果?,java,Java,我在Java中有两个类,我试图使用一个类的结果作为第二个类的值,这就是我所拥有的 DBCall.java- package com.example.tests; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.concurre

我在Java中有两个类,我试图使用一个类的结果作为第二个类的值,这就是我所拥有的

DBCall.java-

package com.example.tests;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.concurrent.TimeUnit;

public class DBCall {

public void generateid(){

    int count;


    try {
        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        Connection conn = DriverManager.getConnection("jdbc:sqlserver://IP:PORT;DatabaseName=SubscriberManager;integratedSecurity=true;");
        System.out.println("Connection Successfull");
        System.out.println(conn); 

        //--------------------------------------------------------------------
        Statement stmt = conn.createStatement();
        String query1= "SELECT COUNT(*) from dbo.vSubscriberReporting where [SubscriberEmailAddressStatusID] in (5,1,6,7) and [SubscriberStatusID] = 1 AND [SubscriberAddresseeStatusID] = 1 and CompanySizeCodeID IN (4)";
        ResultSet rs= stmt.executeQuery(query1);

        if(rs.next()) //Expecting one row.
        {
    count = rs.getInt(1);
             System.out.println("Value: " + count);

        }
    } catch (ClassNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    }

public static void main(String[] args) {
    DBCall gen = new DBCall();
    gen.generateid();

}}
QuickJavaTest.java

package com.example.tests;

import java.util.concurrent.TimeUnit;
import org.junit.*;
import static org.junit.Assert.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;

public class QucikJavaTest {
 private WebDriver driver;
 private String baseUrl;
 private boolean acceptNextAlert = true;
 private StringBuffer verificationErrors = new StringBuffer();

@Before
public void setUp() throws Exception {
 driver = new FirefoxDriver();
baseUrl = "https://testurl";
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}

@Test
public void ConfirmValue7() throws Exception {
driver.get(baseUrl + "/web/SubscriberManagement/Admin");
new Select(driver.findElement(By.id("SelectedFilter"))).selectByVisibleText("Job Title");
driver.findElement(By.id("Filters_0__Values_GB")).click();
driver.findElement(By.id("Filters_0__Values_AF")).click();
driver.findElement(By.cssSelector("button")).click();
for (int second = 0;; second++) {
    if (second >= 60) fail("timeout");
    try { if (isElementPresent(By.id("Filters_1__Value"))) break; } catch (Exception e) {}
    Thread.sleep(1000);
}

driver.findElement(By.xpath("(//a[contains(text(),'Delete')])[2]")).click();
new Select(driver.findElement(By.id("SelectedFilter"))).selectByVisibleText("Job Function");
driver.findElement(By.cssSelector("button")).click();
for (int second = 0;; second++) {
    if (second >= 60) fail("timeout");
    try { if (isElementPresent(By.id("Filters_1__Values"))) break; } catch (Exception e) {}
    Thread.sleep(1000);
}

driver.findElement(By.id("Filters_1__Values_28")).click();
driver.findElement(By.cssSelector("button.floatRight")).click();
driver.findElement(By.id("sqlDetailsHeading")).click();
try {
  assertEquals("7", driver.findElement(By.cssSelector("span.count")).getText());
} catch (Error e) {
  verificationErrors.append(e.toString());
}
 }

@After
public void tearDown() throws Exception {
driver.quit();
String verificationErrorString = verificationErrors.toString();
if (!"".equals(verificationErrorString)) {
  fail(verificationErrorString);
}
 }

 private boolean isElementPresent(By by) {
try {
  driver.findElement(by);
  return true;
} catch (NoSuchElementException e) {
  return false;
}
 }

public boolean isAcceptNextAlert() {
return acceptNextAlert;
}

public void setAcceptNextAlert(boolean acceptNextAlert) {
this.acceptNextAlert = acceptNextAlert;
}
}
我想做的是从DBCall.java中获取count的值,并将其用作QuickJavaTest.java中assertEquals7的验证值,我确信这很简单,我无法找到最好的方法。

您需要从数据库返回generateid方法中获得的值,然后您可以根据需要在其他方法中使用它

将方法从返回void更改为返回int:

然后,使用所需的方法检索数据:

@Test
public void ConfirmValue7() throws Exception {
    DBCall dbCall = new DBCall();
    int count = dbCall.generateid();
    //use count as you want/need
    //rest of your code...
}

为什么不从generateid返回int值呢?我已经做了更改,现在控制台显示-Error:Main方法未在com.example.tests.DBCall类中找到,请将Main方法定义为:public static void mainString[]args或JavaFX应用程序类必须扩展JavaFX.application。Application@user3768497你至少明白你必须做什么吗?您至少了解基本Java吗?@user3768497从您的评论中,您似乎删除或更改了DBCall类中的main方法,这在我的回答中根本没有涉及。好的,我看到了问题所在并解决了它,我对Java和Selenium非常陌生,如果您对我阅读的资源有想法,我很乐意,谢谢你的帮助:@user3768497开始一次学习一个。我建议您先学习Java,最好的地方是跟随。
@Test
public void ConfirmValue7() throws Exception {
    DBCall dbCall = new DBCall();
    int count = dbCall.generateid();
    //use count as you want/need
    //rest of your code...
}