---Advertisement---

Database Testing with Selenium: How to Connect & Validate Data Using JDBC Best 2025

By Manisha

Updated On:

---Advertisement---
Database Testing with Selenium

Introduction

Database Testing with Selenium: Selenium WebDriver is mainly used for UI automation, but database testing is also crucial for validating backend data. To interact with databases in Selenium, we use JDBC (Java Database Connectivity). JDBC allows Java applications to connect to various databases like MySQL, Oracle, PostgreSQL, and SQL Server.

Why Use Database Testing in Selenium?

Verifies database integrity – Ensures data accuracy after UI actions.
Reduces manual testing – Automates database verification.
Detects data discrepancies – Helps in identifying missing or incorrect records.
Improves end-to-end testing – Ensures frontend and backend sync.


👉How to Connect a Database in Selenium?

Database testing with Selenium involves three key steps:
1️⃣ Connect to the Database using JDBC.
2️⃣ Execute SQL Queries to retrieve data.
3️⃣ Process and Validate Results in Java.


Database Testing with Selenium: To connect Selenium with a database, follow this syntax:

java Connection con = DriverManager.getConnection("jdbc:<dbtype>://<ip>:<port>/<db_name>", "username", "password");

Example: Connecting to MySQL Database

Prerequisites

📌 Install MySQL Server & MySQL Workbench
📌 Download MySQL JDBC Driver from
📌 Add MySQL JDBC JAR to Java Project

Code for MySQL Connection

javaimport java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class DatabaseConnection {
public static void main(String[] args) {
String url = "jdbc:mysql://localhost:3306/emp";
String user = "root";
String password = "admin";

try {
Connection con = DriverManager.getConnection(url, user, password);
if (con != null) {
System.out.println("Database Connected Successfully!");
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}

🔹 localhost:3306 – MySQL Server is running locally on port 3306.
🔹 emp – Database name.
🔹 root / admin – Username and password for MySQL.


Database Testing with Selenium: Once connected, we can execute SELECT, INSERT, UPDATE, DELETE queries using Statement Object.

Example: Fetch Data from MySQL Table

javaimport java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class DatabaseTesting {
public static void main(String[] args) {
String url = "jdbc:mysql://localhost:3306/emp";
String user = "root";
String password = "admin";

try {
Connection con = DriverManager.getConnection(url, user, password);
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM employee");

while (rs.next()) {
System.out.println("Name: " + rs.getString("Name") + ", Age: " + rs.getInt("Age"));
}

con.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}

🔹 executeQuery("SELECT * FROM employee") – Retrieves all records from the employee table.
🔹 rs.getString("Name") / rs.getInt("Age") – Fetches column values.


Now, let’s integrate Selenium WebDriver with database validation.

Example: Verify Login Functionality

javaimport org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class SeleniumDatabaseTest {
public static void main(String[] args) throws Exception {
WebDriver driver = new ChromeDriver();
driver.get("https://example.com/login");

// Database Connection
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/emp", "root", "admin");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT username, password FROM users WHERE id = 1");

rs.next();
String dbUsername = rs.getString("username");
String dbPassword = rs.getString("password");

// Enter credentials from database into login page
driver.findElement(By.id("username")).sendKeys(dbUsername);
driver.findElement(By.id("password")).sendKeys(dbPassword);
driver.findElement(By.id("login")).click();

System.out.println("Login test executed with database credentials!");

driver.quit();
con.close();
}
}

Compares UI input with database values
Ensures correct login details are stored in DB


1️⃣ Insert Data into Database

javastmt.executeUpdate("INSERT INTO employee (Name, Age) VALUES ('John', 30)");

2️⃣ Update Data in Database

java stmt.executeUpdate("UPDATE employee SET Age = 35 WHERE Name = 'John'");

3️⃣ Delete Data from Database

javastmt.executeUpdate("DELETE FROM employee WHERE Name = 'John'");

Step 1 – Connect to the Database using JDBC.
Step 2 – Execute SQL queries using Statement object.
Step 3 – Process results using ResultSet.
Step 4 – Compare database values with UI elements in Selenium.


Database testing with Selenium ensures data consistency, integrity, and reliability across UI and backend. By integrating JDBC with Selenium WebDriver, you can automate database validation, making your testing more efficient and accurate.


Need help setting up Selenium Database Testing? Drop your questions below!

MYSQL Download

Selenium Frame Work

Leave a Comment

Index