Class DatabaseActions

java.lang.Object
com.shaft.db.DatabaseActions
Direct Known Subclasses:
SHAFT.DB

public class DatabaseActions extends Object
Manages database connections and query execution for supported relational database systems.

Supports MySQL, SQL Server, PostgreSQL, Oracle (SID or service name), and IBM DB2 via JDBC. Provides methods for executing SELECT queries and retrieving results as ResultSet objects.

Thread safety: Each instance maintains per-thread result sets via ThreadLocal to support parallel test execution.

Usage example:

SHAFT.DB db = new SHAFT.DB(DatabaseActions.DatabaseType.MY_SQL,
        "localhost", "3306", "testdb", "user", "pass");
ResultSet rs = db.executeSelectQuery("SELECT * FROM users");
See Also:
  • Constructor Details

    • DatabaseActions

      public DatabaseActions(DatabaseActions.DatabaseType databaseType, String ip, String port, String name, String username, String password)
      This constructor is used for initializing the database variables that are needed to create new connections and perform queries
      Parameters:
      databaseType - database type that you want to connect with: DatabaseType.MY_SQL ,SQL_SERVER,POSTGRES_SQL.
      ip - IP address that has database installation that we need to connect to (e.g. 72.55.136.25)
      port - port of database installation on the server (e.g. 3306)
      name - database name that you need to connect to
      username - database username
      password - password of database user
    • DatabaseActions

      public DatabaseActions(String customConnectionString)
      This constructor is used for initializing the database variables that are needed to create new connections and perform queries
      Parameters:
      customConnectionString - custom database connection string ex: "jdbc:oracle:thin:@dbServerIP:dbPort:dbName"
  • Method Details

    • getInstance

      public static DatabaseActions getInstance(DatabaseActions.DatabaseType databaseType, String ip, String port, String name, String username, String password)
      Creates a database actions instance using structured connection parameters.
      Parameters:
      databaseType - the target database type
      ip - the database server host or IP address
      port - the database server port
      name - the target database name
      username - the database username
      password - the database password
      Returns:
      a configured database actions instance
    • getInstance

      public static DatabaseActions getInstance(String customConnectionString)
      Creates a database actions instance using a raw JDBC connection string.
      Parameters:
      customConnectionString - full JDBC connection string
      Returns:
      a configured database actions instance
    • getResult

      public static String getResult(ResultSet resultSet)
      Returns a string representation of the provided resultSet object
      Parameters:
      resultSet - the object returned as a result of performing a certain database query
      Returns:
      a string value which represents the provided resultSet object
    • getRow

      public static String getRow(ResultSet resultSet, String columnName, String knownCellValue)
      Returns a string value which represents the data of the target row
      Parameters:
      resultSet - the object returned as a result of performing a certain database query
      columnName - the name of the column holding the knownCellValue
      knownCellValue - a value that the engine searches for under the specified columnName, when that value is found, the row that contains it is read and added to the returned string
      Returns:
      a string value which represents the data of the target row
    • getColumn

      public static String getColumn(ResultSet resultSet, String columnName)
      Returns a string value which represents the data of the target column
      Parameters:
      resultSet - the object returned as a result of performing a certain database query
      columnName - the name of the target column that will be read
      Returns:
      a string value which represents the data of the target column
    • getRowCount

      public static int getRowCount(ResultSet resultSet)
      Returns the number of rows contained inside the provided resultSet
      Parameters:
      resultSet - the object returned as a result of performing a certain database query
      Returns:
      an integer value which represents the number of rows contained inside the provided resultSet
    • getResult

      public String getResult()
      Returns the latest thread-local SELECT result as a text table.
      Returns:
      string representation of the latest result set
    • getRow

      public String getRow(String columnName, String knownCellValue)
      Returns rows whose columnName matches knownCellValue from the latest result set.
      Parameters:
      columnName - the column used for matching
      knownCellValue - the expected cell value in the target column
      Returns:
      tab-separated row data matching the requested value
    • getColumn

      public String getColumn(String columnName)
      Returns all values from a column in the latest thread-local result set.
      Parameters:
      columnName - the column to read
      Returns:
      newline-separated values from the target column
    • getRowCount

      public int getRowCount()
      Returns the number of rows in the latest thread-local result set.
      Returns:
      row count for the latest executed query
    • executeSelectQuery

      public ResultSet executeSelectQuery(String sql)
      Executes a SELECT statement and returns the result as a ResultSet object
      Parameters:
      sql - an SQL statement to be sent to the database, typically a static SQL SELECT statement
      Returns:
      a ResultSet object that contains the data produced by the given query; never null
    • executeUpdateQuery

      public int executeUpdateQuery(String sql)
      Executes any DML or DDL statement and returns the result as a ResultSet object
      Parameters:
      sql - an SQL Data Manipulation Language (DML) ;UPDATE statement, or an SQL statement that returns nothing, such as a DDL statement.
      Returns:
      either (1) the row count for SQL Data Manipulation Language (DML) statements or (2) 0 for SQL statements that return nothing
    • executeDDLStatement

      public void executeDDLStatement(String sql)
      Executes a DDL statement (for example, CREATE/ALTER/DROP).
      Parameters:
      sql - the DDL SQL statement to execute
    • executeInsertQuery

      public int executeInsertQuery(String sql)
      Executes any DML or DDL statement and returns the result as a ResultSet object
      Parameters:
      sql - an SQL Data Manipulation Language (DML) ;INSERT statement, or an SQL statement that returns nothing, such as a DDL statement.
      Returns:
      either (1) the row count for SQL Data Manipulation Language (DML) statements or (2) 0 for SQL statements that return nothing
    • executeDeleteQuery

      public int executeDeleteQuery(String sql)
      Executes any DML or DDL statement and returns the result as a ResultSet object
      Parameters:
      sql - an SQL Data Manipulation Language (DML) ;DELETE statement, or an SQL statement that returns nothing, such as a DDL statement.
      Returns:
      either (1) the row count for SQL Data Manipulation Language (DML) statements or (2) 0 for SQL statements that return nothing
    • cleanup

      public void cleanup()
      Removes all thread-local state held by this instance, preventing memory leaks in long-running or parallel test environments. Call this in teardown methods (e.g., @AfterMethod) after all database interactions are complete.