Oracle Database Connectivity (WebDriver) - Bug Reaper

                  Bug Reaper

Lean about Automation Testing,Selenium WebDriver,RestAssured,Appium,Jenkins,JAVA,API Automation,TestNG,Maven, Rest API, SOAP API,Linux,Maven,Security Testing,Interview Questions

Friday, 27 September 2013

Oracle Database Connectivity (WebDriver)

Code Snippet for Oracle Database Connectivity in WebDriver
Jar Files required:
1.)ojdbc14-10.2.0.3.0.jar
2.)mysql-connector-java-5.1.21.jar

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

/**
 * @author www.javaworkspace.com
 *
 */
public class Db {
public static String sql = "";
public static String firstname="hjhj";
public static String sql1 = "";
public static String xyz = "";

public static void main(String[] args1) throws SQLException  {


System.out.println("-------- Oracle JDBC Connection Testing ------");
try {

DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
}
  catch (SQLException e)
       {
           System.out.println("Oracle JDBC Driver not found !!");
           e.printStackTrace();
           return;
       }
System.out.println("Oracle JDBC Driver Registered!");

   // Create Connection Object
Connection connection=null;
 try
       {
             connection = DriverManager.getConnection(
                    "jdbc:oracle:thin:@ Hostname:1521:SID", "Username", "Password");

       }

 catch (SQLException e)
       {
           System.out.println("Connection Failed! Check output console");
           e.printStackTrace();
           return;
       }

  if (connection != null)
       {
           System.out.println("Database Connection Successful !! ");
       }
       else
       {
           System.out.println("Connection Failed !!");
       }
     
   // Declaring Statement type variable
            Statement statement = connection.createStatement();
         
            // Storing result in Result Set type variable
            ResultSet resultSet = statement
                    .executeQuery("SELECT * FROM (select * from  abc ORDER BY UPDATED_DATE DESC) WHERE ROWNUM = 1");
         // Extracting data from result set
            while (resultSet.next()) {
                System.out.println("SETUPID:"
                        + resultSet.getString("ID"));
                 sql=resultSet.getString("ID");
             
            }
            ResultSet resultSet2 = statement
                    .executeQuery("SELECT abc FROM xyz WHERE FIRST_NAME LIKE '%" + firstname + "%'");
            while (resultSet2.next()) {
                System.out.println("abc:"
                        + resultSet2.getString("abc"));
                 xyz=resultSet2.getString("abc");
             
            }
            ResultSet resultSet1 = statement
                    .executeQuery("SELECT ID FROM EMPLOYEE_DATA where UPDATED_BY='user'");
            while (resultSet1.next()) {
                System.out.println("EMPLOYEE ID:"
                        + resultSet1.getString("ID"));
                 sql1=resultSet1.getString("ID");
             
            }
         
         


}  }

2 comments:

  1. Hi Neeraj Ji,
    Please keep posting content on Selenium(WebDriver) and on S/W Automation.
    Please keep up the Good Work.
    Thank You.

    ~Chaitanya

    ReplyDelete
  2. The SQL statements that read data from a database query return the data in a result set.
    A ResultSet object maintains a cursor that points to the current row in the result set. The term "result set" refers to the row and column data contained in a ResultSet object.

    JDBC provides following connection methods to create statements with desired ResultSet:

    createStatement(int RSType, int RSConcurrency);

    prepareStatement(String SQL, int RSType, int RSConcurrency);

    The ResultSet interface provides getter methods (getBoolean, getLong, and so on) for retrieving column values from the current row.

    ReplyDelete