witam
w mojej aplikacji potrzebuje na poczatku dodac do tabeli wiersz gdzie id = auto increament;
Nastepnie bede musial wykonac testy przy uzyciu selenium driver
Po wykonanym tescie musze usunac ostatnio zapamietany, dodany wiersz.
ps. nie wchodzi w gre dodanie sobie wiersza z id=10000 a pozniej usuniecie go na podstawie id=10000, oraz przypadek w ktorym usuniety zostanie najwiekszy id gdyz test moze trwac 20 sekund, a w tym czasie moga zostac dodane rekordy do bazy. Bede bardzo wdzieczny za pomoc
moj log z konsoli poki co wyglada tak
Connected database successfully...
Key returned from 'delete LAST_INSERT_ID()': 21084
PASSED: test
package tutorialselenium;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
import page.classes.itv_pom;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeClass;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
// Sequel Pro - for Database
// Driver Download - https://dev.mysql.com/downloads/connector/odbc/
public class DatabaseTesting {
// Connection object
Connection conn = null;
// Statement object
private static Statement stmt;
// Result Set
private static ResultSet rs = null;
// Constant for Database URL
public static String DB_URL = "jdbc:mysql://localhost:3306/";
// Constant for Database Username
public static String DB_USER = "admin";
// Constant for Database Password
public static String DB_PASSWORD = "password";
// Driver
public static String driver = "com.mysql.jdbc.Driver";
// WebDriver
// public static WebDriver dv;
@BeforeClass
public void beforeClass() {
// Properties for creating connection to database
Properties props = new Properties();
props.setProperty("user", "root");
props.setProperty("password", "");
try {
Class.forName("com.mysql.jdbc.Driver");
System.out.println("Connecting to a selected database...");
conn = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD);
System.out.println("Connected database successfully...");
} catch (Exception e) {
e.printStackTrace();
}
}
@Test
public void test() throws SQLException {
try {
stmt = conn.createStatement();
rs = stmt.executeQuery("SELECT * FROM itv_cs.autoIncTutorial");
stmt.executeUpdate("INSERT INTO itv_cs.autoIncTutorial (dataField) " + "values ('auto here?')",
Statement.RETURN_GENERATED_KEYS);
int autoIncKeyFromFunc = -1;
rs = stmt.executeQuery("SELECT LAST_INSERT_ID()");
if (rs.next()) {
autoIncKeyFromFunc = rs.getInt(1);
} else {
// throw an exception from here
}
System.out.println("Key returned from " + "'SELECT LAST_INSERT_ID()': " + autoIncKeyFromFunc);
rs.close();
} catch (SQLException se) {
// Handle errors for JDBC
se.printStackTrace();
} catch (Exception e) {
// Handle errors for Class.forName
e.printStackTrace();
}
}
@AfterClass
public void afterClass() {
try {
if (rs != null)
rs.close();
if (stmt != null)
conn.close();
if (conn != null)
conn.close();
} catch (SQLException se) {
se.printStackTrace();
}
}
}