JDBC for Selenium

JDBC

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.
DriverManager
  • 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).
Driver
  • 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
  • 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");
while(resset.next())
{
	System.out.println("Username: "+resset.getString(1));
	System.out.println("password : "+resset.getString(2));
}



Other Posts

JDBC for Selenium

jdbc for selenium, jdbc driver for selenium, jdbc jar for selenium, jdbc api for selenium,...

Selenium grid

Selenium Grid is a server that allows tests to use web browser instances running on...

GIT

Understanding git fetch commandgit fetch origingit fetch command only downloads new data from a remote...

GIT

Get detail about a particular commitTo get the details about particular commit, you can do...

Java Interview Questions

Java Interview Questions Below are most frequently asked question in Interview for Java topic. Now...

Leave a Reply

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