Connect to DataBase in Java and Selenium Implementation

For initial understanding on database and SQL queries,
refer MySQL Basics

To automate database testing, we will use selenium with java language.

JDBC (Java DataBase Connectivity) is an API (using Java language) to connect and play with database with SQL queries.

In this post, let’s understand how we can connect with database using Java, and how we can implement in selenium automation testing.

JDBC Driver : To connect to the database (Relational Database)

JDBC DriverManager : DriverManager is a class, manages list of all database drivers. it connects java applications(java code) to a proper JDBC driver.

Connection: Creates channel to database.

Statement: These objects are to send SQL queries to database.

ResultSet: Object to hold retrieved results (set of Rows) from SQL queries.

Jar files required for DataBase connectivity:
We need mysql-connector-java-X.X.XX-bin.jar file for the JDBC with MySQL.
Download and install from (Jar file will be located under C:Program Files (x86)MySQLMySQL Connector J )

Note: add the jar file to your eclipse project -> Java build path

Load the DB Server:

Setup connection with DB:

Rest of code and SQL queries are almost same for all kind of databases
Let’s see MySQL database connectivity example:

Now refresh your project and observe the 2 screenshots generated, 1.jpg and 2.jpg to validate if we have same data as the database.

Content posted is based on learning or working experience,
Share the post with others if find useful,
Any queries?, leave comments or discuss on our, Thanks!

1 Response

  1. Vishal Patel says:

    Kindly, provider such detailed example for oracle database.

Leave a Reply

Your email address will not be published. Required fields are marked *