All the applications need a database to store the data. In databases like MYSQL, SQL Server, and Oracle, Database Testing is performed to test the web application in conjunction with a database to ensure that the changes are reflected at both ends.
To test the database using the selenium we need JDBC (“Java Database Connectivity”). JDBC is a SQL level API that allows us to connect Java with the database and allows us to execute the SQL statements.
Why we need Database testing ?
- To ensure that data entered from the UI is consistently reflected in the database.
- When a user loads the test data or expected data from the Database.
- To ensure that changes updated in the data reflected in the database.
Steps to perform database testing:
- create a connection with the database.
- Execute the queries.
- Disconnect the database.
Code with description:
package databaseTesting; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class DatabaseTesting { public static void main(String[] args) throws SQLException, ClassNotFoundException { //Intialialization of variables String url = "jdbc:mysql://doctor.cdzl.us-east-1.rds.amazonaws.com:3306/?user=doctor"; String username = "doctor"; String password = "doctor123"; //Loading the required MYSQL JDBC Driver class Class.forName("com.mysql.jdbc.Driver"); //Creating a connection to the database Connection conn = DriverManager.getConnection(url, username, password); //Executing SQL query and fetching the result Statement fetchData = conn.createStatement(); String excuteComm= "SELECT * FROM Doctor.Patients"; ("SELECT * FROM DatabaseName.Table") ResultSet getResult= fetchData .executeQuery(excuteComm); while (getResult.next()) { System.out.println("Patient_Id: " + getResult.getString("Patient_Id")); (Get the Column data) } //disconnect the database fetchData.close(); } }
Result Screenshot:
2 comments