我需要從下面的方法調用一些字符串(如用戶名)。包名稱是login,數據庫方法是SQLConnector,測試用例是Firstlogin。我需要調用數據庫的Firstlogin連接使用,從它串在Firstlogin和Firstlogin過於執行查詢:在我需要使用數據庫連接在第二種方法從Java方法調用數據庫oracle連接到另一個方法
package login;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class SQLConnector {
static Connection con = null;
private static Statement stmt;
public static String DB_URL = "jdbc:oracle:thin:@10.96.0.65:1521:orcl";
public static String DB_USER = "POS_SOF";
public static String DB_PASSWORD = "POS_SOF";
static String username;
@Before
public void setUp() throws Exception {
try{
String dbClass = "oracle.jdbc.driver.OracleDriver";
Class.forName(dbClass).newInstance();
Connection con = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD);
stmt = con.createStatement();
}
catch (Exception e)
{
e.printStackTrace();
}
}
@Test
public void test() {
try{
String query = "select * from users where id = '45450'";
String expectedEmpName = "test1234";
ResultSet res = stmt.executeQuery(query);
while (res.next())
{
username = res.getString("user_name");
System.out.print(username);
assertEquals(expectedEmpName, username);
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
@After
public void tearDown() throws Exception {
if (con != null) {
con.close();
}
}
} :
package login;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import java.util.concurrent.TimeUnit;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.Select;
import login.SQLConnector;
public class FirstLogin {
private String baseUrl;
private boolean acceptNextAlert = true;
private StringBuffer verificationErrors = new StringBuffer();
@Before
public void setUp() throws Exception {
System.setProperty("webdriver.chrome.driver", "D://DownLoads/chromedriver_win32/chromedriver.exe");
driver = new ChromeDriver();
baseUrl = "https://100.96.0.650:9443";
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
}
@Test
public void testAddAccount() throws Exception {
driver.get(baseUrl + "/POSAdminTool/AdminTool/SearchPOS.faces");
driver.manage().window().maximize();
driver.findElement(By.id("form1:usernameLabel")).clear();
driver.findElement(By.id("form1:usernameLabel")).sendKeys(SQLConnector.username);
driver.findElement(By.id("form1:passwordLabel")).clear();
driver.findElement(By.id("form1:passwordLabel")).sendKeys("1234");
driver.findElement(By.id("form1:btn_login")).click();
Thread.sleep(1000);
Actions action = new Actions(driver);
WebElement element = driver.findElement(By.cssSelector("html body table.mainTable tbody tr td p.menuItem a"));
action.moveToElement(element);
action.click();
action.perform();
driver.findElement(By.linkText("Add Account")).click();
driver.findElement(By.id("addPOS:locationID")).clear();
driver.findElement(By.id("addPOS:locationID")).sendKeys("9999");
driver.findElement(By.id("addPOS:menu1")).clear();
driver.findElement(By.id("addPOS:menu1")).sendKeys("10000");
driver.findElement(By.id("addPOS:menuStatus1")).click();
new Select(driver.findElement(By.id("addPOS:usageList"))).selectByVisibleText("Pharmacy");
driver.findElement(By.id("addPOS:textareaDescription1")).clear();
driver.findElement(By.id("addPOS:textareaDescription1")).sendKeys("New");
driver.findElement(By.id("addPOS:addAcctTerminal")).click();
new Select(driver.findElement(By.id("AddAcctTerminalData:statusList"))).selectByVisibleText("Active");
new Select(driver.findElement(By.id("AddAcctTerminalData:TermList"))).selectByVisibleText("Point of Sale");
driver.findElement(By.id("AddAcctTerminalData:textSN1")).clear();
driver.findElement(By.id("AddAcctTerminalData:textSN1")).sendKeys("22-55-88");
driver.findElement(By.id("AddAcctTerminalData:textPin1")).clear();
driver.findElement(By.id("AddAcctTerminalData:textPin1")).sendKeys("1234");
driver.findElement(By.id("AddAcctTerminalData:add")).click();
driver.findElement(By.id("addPOS:textDailyLimit1")).clear();
driver.findElement(By.id("addPOS:textDailyLimit1")).sendKeys("10000");
driver.findElement(By.id("addPOS:textCreditLimit1")).clear();
driver.findElement(By.id("addPOS:textCreditLimit1")).sendKeys("10000");
driver.findElement(By.id("addPOS:button1")).click();
assertEquals("Account Added Successfully", driver.findElement(By.id("AddAccountSuccess:CorrectMessage")).getText());
String AddedAccount = "SELECT CODE FROM ACCOUNTS WEHER ID IN (SELECT MAX(ID) FROM ACCOUNTS)";
String AccountCode = driver.findElement(By.id("AddAccountSuccess:AccountCode")).getText();
System.out.print(AccountCode);
System.out.print(AddedAccount);
assertEquals(AddedAccount, AccountCode);
}
@After
public void tearDown() throws Exception {
driver.quit();
String verificationErrorString = verificationErrors.toString();
if (!"".equals(verificationErrorString)) {
fail(verificationErrorString);
}
}
}
這裏是我做過什麼: 公共連接CON = NULL; con = DriverManager.getConnection(DB_URL,DB_USER,DB_PASSWORD); 但仍然是同樣的問題 –