JDBC for Selenium


Java Database Connectivity is an application programming interface for the programming language Java, which defines how a client may access a database. It is a Java-based data access technology used for Java database connectivity.

JDBC for Selenium
  • We may be required to fetch data sets from any sort of data pools like excel sheets, databases, properties files etc.
  • Java Database Connectivity (JDBC) used to establish connection with databases and fetch the same for parameterizing our test scripts.
  • JDBC) is an application programming interface (API) for the programming language Java, which defines how a client can access a database.
  • It act like a bridge between the Java program and a wide range of databases.
  • It acts as an interface between user and drivers.
  • It’s main job is to keeps track of the drivers that are available and handles establishing a connection.
  • Method to establish connection with the database is  getconnection(String URL).
  • It is a software component that enables java application to interact with the database.
  •  Thin drivers are used for establishing the connection as it converts JDBC calls directly into the vendor-specific database protocol and hence known as thin driver.
  • Connection is the session between java application and database.
  • It provides methods for the creation of statement instances and for managing transactions in database interactions.
  • getConnection() method returns an instance of Connection.
  • Oracle Example :
Connection scon=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","oracle");
Statement stmt=con.createStatement();
  • SQL Server Example code :
Connection con=DriverManager.getConnection("jdbc:sqlserver://*****;user=***;password=****;database=****");
Statement stmt=con.createStatement();
  • MySQL Example code
connection = DriverManager.getConnection("jdbc:mysql://****:****/****","Username", "Password");	
Statement stmt=connection.createStatement();
Statement interface
  • public ResultSet executeQuery(String sql) is used to execute SELECT query.
  • public int executeUpdate(String sql) is used to execute a specified query. Its may be a create, drop, insert, update, delete statements.
  • public boolean execute(String sql) is used to execute queries that may return multiple results.
  • public int[] executeBsatch() is used to execute batch of SQL commands.
Selenium WebDriver code
Below is Selenium code for MySQL query –select * from Test;
ResultSet resset=stmt.executeQuery("select * from Test");
	System.out.println("Username: "+resset.getString(1));
	System.out.println("password : "+resset.getString(2));

Leave a Reply

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