JDBC (Java Database Connectivity) is a SQL level API that allows you to execute SQL statements. It is responsible for the connectivity between the Java Programming language and a wide range of databases. The JDBC API provides the following classes and interfaces

Driver Manager Driver Connection Statement ResultSet SQLException

In this tutorial, you will learn

Make a connection to the Database Send Queries to the Database Process the results Example of Database Testing with Selenium

In order to test your Database using Selenium, you need to observe the following 3 steps

1) Make a connection to the Database

In order to make a connection to the database the syntax is DriverManager.getConnection(URL, “userid”, “password” ) Here,

Userid is the username configured in the database Password of the configured user URL is of format jdbc:< dbtype>://ipaddress:portnumber/db_name” - The driver for the database you are trying to connect. To connect to oracle database this value will be “oracle”For connecting to database with name “emp” in MYSQL URL will bejdbc:mysql://localhost:3036/emp

And the code to create connection looks like You also need to load the JDBC Driver using the code

2) Send Queries to the Database

Once connection is made, you need to execute queries. You can use the Statement Object to send queries. Once the statement object is created use the executeQuery method to execute the SQL queries

3) Process the results

Results from the executed query are stored in the ResultSet Object. Java provides loads of advance methods to process the results. Few of the methods are listed below

Example of Database Testing with Selenium

Step 1) Install MySQL Server and MySQL Workbench Check out the complete guide to Mysql & Mysql Workbench here While installing MySQL Server, please note the database

Username Password Port Number

It will be required in further steps. MySQL Workbench makes it easy to administer the database without the need to code SQL. Though, you can also use the MySQL Terminal to interact with the database.

In the next screen,

Select Local Instance of MySQL Enter Port Number Enter Username Enter Password Click OK

Step 3) To Create Database,

Click create Schema Button Enter Name of Schema/Database Click Apply

Step 4) In the navigator menu,

Click on Tables, beneath the emp database Enter Table name as employee Enter Fields as Name and Age Click Apply

You will see the following pop-up. Click Apply

In navigator, select the employee table In right pane, click Form Editor Enter Name and Age Click Apply

Repeat the process until all data is created

Step 6) Download the MySQL JDBC connector here

Step 7) Add the downloaded Jar to your Project

Right click on your Java File. Then click on Build Pathà Configure build path Select the libraries Click on add external JARs You can see MySQL connector java in your library Click on open to add it to the project

Step 8) Copy the following code into the editor Step 8) Execute the code, and check the output

Summary of Steps for Selenium Database Testing

Step 1) Make a connection to the Database using method. Step 2) Create Query to the Database using the Statement Object. Step 3) Send the query to database using execute query and store the results in the ResultSet object. Java provides lots of built-in methods to process the> SQL Output using the ResultSet Object