My Thoughts: Connecting to DataBase using WebDriver

Thursday, January 5

Connecting to DataBase using WebDriver

Our scripts are not just clicking on links we will do more.Yes , of course more :) and one of that "more" is "checking DB".
But Web Driver cannot directly connect to Database. You can only interact with your Browser using Web Driver.SO if you want to connect to Database then you need to write piece of code which will let you to connect to Database to perform further actions(insertion, deletion, updation).

For this we use JDBC("Java Database Connectivity").The JDBC API is a Java API for accessing virtually any kind of tabular data.The value of the JDBC API is that an application can access virtually any data source and run on any platform with a Java Virtual Machine.

In simplest terms, a JDBC technology-based driver ("JDBC driver") makes it possible to do three things:

1.Establish a connection with a data source
2.Send queries and update statements to the data source
3.Process the results

1.Establish a connection with a data source
The traditional way to establish a connection with a database is to call the method
DriverManager.getConnection(URL,  "myLogin", "myPassword" )
URL :   jdbc:<subprotocol>:<subname>
<subprotocol>-the name of the driver or the name of a database connectivity mechanism
<subname> - The point of a subname is to give enough information to locate the data source .(Includes IP address , Port number and exact name of DataSource)

For connecting to MYSQL URL will be

2.Send queries and update statements to the data source
A Statement object is used to send SQL statements to a database over the created connection in Step 1.
Statement-created by the Connection.createStatement methods. A Statement object is used for sending SQL statements with no parameters.
PreparedStatement-created by the Connection.prepareStatement methods. A PreparedStatement object is used for precompiled SQL statements. These can take one or more parameters as input arguments (IN parameters).
CallableStatement-created by the Connection.prepareCall methods. CallableStatement objects are used to execute SQL stored procedures
In Short
createStatement methods-for a simple SQL statement (no parameters)
prepareStatement methods-for an SQL statement that is executed frequently
prepareCall methods-for a call to a stored procedure

3.Process the results
A ResultSet is a Java object that contains the results of executing an SQL query.We will have separate post on it.The JDBC API provides three interfaces for sending SQL statements to the database

Here is the code for it.

//Load the mysql driver dynamically
//Establish connection
Connection con = DriverManager.getConnection("jdbc:mysql:wombat", "myLogin", "myPassword");
//Create statement Object
Statement stmt = con.createStatement();
//Execute the query and store the results in the ResultSet object
ResultSet rs = stmt.executeQuery("SELECT id, name, salary FROM employee");
//Printing the column values of ResultSet
while ( {
 int x = rs.getInt("id");
 String s = rs.getString("name");
 float f = rs.getFloat("salary");


  1. This post explains clearly about DB connetions ...Its very neat and simple ..good work..keep posting new things like...looking forward for your new posts