If You use oracle, to find out what Your URL is, You can do the following. There is a tnsnames.ora file which defines database addresses. This file is usually located in $ORACLE_HOME/network/admin and is used by oracle clients. Here is a sample tns entry:
ORA11 = (DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = hostname)(PORT = 1521)) ) (CONNECT_DATA = (SERVICE_NAME = ORA11) ) )
From this entry you can work out that your jdbc connection string would be:
jdbc:oracle:thin:@hostname:1521:ORA11
If You want to connect to db via java bellow is an example. First You have to have some kind of database server to which You will connect in Your case Oracle. If You would experience any trouble it is because of drivers,
I use maven and this is an dependancy, You might need:
<!-- https://mvnrepository.com/artifact/com.oracle/ojdbc14 -->
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc14</artifactId>
<version>10.2.0.4.0</version>
</dependency>
and here is an example class, You need to tweak:
public class ConnectToDatabse { Connection conn = null; Statement stmt = null; ResultSet resultSet = null; WebDriver driver; @BeforeTest public void SetUpConnection() throws SQLException, ClassNotFoundException { // Register JDBC driver (JDBC driver name and Database URL) Class.forName("oracle.jdbc.driver.OracleDriver"); // Open a connection conn = DriverManager.getConnection("jdbc:oracle:thin:@//HOSTNAME:PORT/SERVICENAME","user", "password"); System.setProperty("webdriver.chrome.driver", "<Path of Driver>\\chromedriver.exe"); ChromeOptions options = new ChromeOptions(); // Code to disable the popup of saved password Map<String, Object> prefs = new HashMap<String, Object>(); prefs.put("credentials_enable_service", false); prefs.put("password_manager_enabled", false); options.setExperimentalOption("prefs", prefs); driver = new ChromeDriver(options); driver.get("<URL>"); }
and this is a test of a sort:
@Test public void testTable() { try { // Execute a query stmt = conn.createStatement(); resultSet = stmt.executeQuery("select * from sampletable"); // Get the all row of UI Table List<WebElement> lstTr = driver.findElement(By.id("grdData")).findElements(By.tagName("tr")); // Index for Row int rowCount = 0; // Count of Matched Column int matchColumnCount = 0; // Count of Matched Row int matchRowCount = 0; System.out.println("Row Count => " + lstTr.size()); // Extract the data from Table while (resultSet.next()) { List<WebElement> lstTd = lstTr.get(rowCount + 1).findElements(By.tagName("td")); System.out.println("Cloumn Count => " + lstTd.size()); for (int j = 0; j < lstTd.size(); j++) { String uiCell = lstTd.get(j).getText(); System.out.println("UI Cell Data => " + uiCell); /* * (j + 1) in the resultSet => because index is start from 1 * and here loop is starting from 0 */ String dbCell = resultSet.getString(j + 1); System.out.println("DB Cell Data => " + dbCell); // Comparison between both string if (uiCell.trim().equalsIgnoreCase(dbCell.trim())) { matchColumnCount++; } } if (matchColumnCount == lstTd.size()) { matchRowCount++; System.out.println("========ROW MATCHED=========="); } } assertEquals(matchRowCount, rowCount, "UI Table is the exact copy of Database Table"); } catch (Exception e) { System.out.println(e); } }
and finally after test is done closing connection with DB.
@AfterTest public void CloseTheConnection() throws SQLException { // Code to close each and all Object related to Database connection if (resultSet != null) { try { resultSet.close(); } catch (Exception e) { } } if (stmt != null) { try { stmt.close(); } catch (Exception e) { } } if (conn != null) { try { conn.close(); } catch (Exception e) { } } driver.quit(); } }
Hope this helps!