Thursday, April 17, 2014

[Selenium Webdriver] Connection to oracle database using Java

There are time during the functional automation when you the test engineer have to connect to the database in order to verify the data driven functionality. For that purpose, it is necessary to connect to the database during test execution to verify the functionality that is dependent on the data coming from the database.

Following is the class that is written in Java to connect to the database that: 

1. creates the connection to the database
2. execute the query on the database
3. close the database connection when the query is executed.

Pre-requisite:

Download oracle ojdbc14.jar or ojdbc6.jar from the internet and configure it to java-build path in your favorite IDE.

Database connection class

package com;

import java.io.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;



public class OrclConn {

private Connection connection;
private String serverName;
private String portNumber;
private String sid;
private String url;
private String username;
private String password;
private Statement stmt;
public ResultSet rset;
 
 /* 
This constructor initializes the database variables that requires to connect to the database
   */
public OrclConn(String serName, String portNum,
String osid, String dbUrl, String dbUserName, String dbPassword ) throws IOException
{
connection = null;
serverName = serName; // pass server name of IP from the calling classs
   portNumber = portNum; // pass port number from the calling classs
   sid = osid; // pass oracle service name from the calling classs
   url = dbUrl + serverName + ":" + portNumber + ":" + sid; // "jdbc:oracle:thin:@" + serverName + ":" + portNumber + ":" + sid;
   username = dbUserName; // pass oracle database name from the calling classs
   password = dbPassword; // pass oracle database password (plaintext) from the calling classs
       
}

public void OpenDBConnection()
{
   try 
{

   // Load the JDBC driver
   String driverName = "oracle.jdbc.driver.OracleDriver";
   Class.forName(driverName);

   // Create a connection to the database
   connection = DriverManager.getConnection(url, username, password);
   }
   
   catch (ClassNotFoundException e) 
{
System.out.println("Class not found from database" );
e.printStackTrace();

catch (SQLException e1) 
{
System.out.println("ORACLE Connection error " );
e1.printStackTrace();
}
       
   
}

 /* This Function execute the query on the connected database and return the ResultSet collection 
Upon callling from the test case class where actual verification is being done on UI and Database.
   */
   public ResultSet RunQuery(String Query) throws IOException
   {
   
    try{
   stmt = connection.createStatement();
   rset = stmt.executeQuery(Query);
   
   }
    catch(SQLException e1)
    {
    System.out.println("Query Execution Error" );
e1.printStackTrace();
    }
   
    return rset;
   }
       
       
       
 /* This Function closes the database connection from the Oracle Database Connection
Upon callling from the test case class where actual verification is being done.
   */
   public void OracleCloseConnection() throws IOException
   {
    try{
   connection.close();
    }
   
    catch(SQLException e1)
    {
    System.out.println("Query Execution Error" );
e1.printStackTrace();
   
    }
   

   
}

How do you connect to the database from the Java?

6 comments:

  1. Good article with the snippet of code to connect the db, executing query and disconnecting db.People willing to get the indepth knowledge in getting knowledge such as TestNG,jenkins,Maven and POM should get properly get best selenium training in chennai

    ReplyDelete
  2. Step by step by information for novice. This really helps! Thank you very much!!

    ReplyDelete
  3. simply superb ,mind blowing, I will share your blog to my friends also
    selenium Online Training

    ReplyDelete
  4. Great post but I was wondering if you could write a little more on this subject? I’d be very thankful if you could elaborate a little bit further. Thanks in advance! Salesflow linked

    ReplyDelete