DbExecutor

DbExecutor provides utility methods for interacting with a database, allowing you to query, insert, update, and delete data.

Methods

queryValue(query: String)

Description: Executes a query on the specified database and returns a single string value. It's used for retrieving specific data from a database.

Parameters:

  • query (type: String): The SQL query to execute.

Returns: A string containing the result of the query.

Example: Get the email address of a user based on their username.

Use Case: Useful for retrieving user contact information from a database

var userEmail = DbUtil.getDbExecutorByName("Sample Database (MySQL)").queryValue("SELECT email from demo501db.users where username like 'Samuel'")

insert(sql: String)

Description: Inserts data into the specified database using an SQL insert statement and returns the number of affected rows. It's used for adding new data records to a database.

Parameters:

  • sql (type: String): The SQL insert statement.

Returns: An Integer representing the number of rows affected by the insert operation.

Example: Insert a new product into the products database.

Use Case: Common in e-commerce systems for adding new items to the product catalog.

var newProductInsert = "INSERT INTO products (name, price) VALUES ('New Product', 50)";
var insertedRowCount = DbUtil.getDbExecutorByName("Sample Database (MySQL)").insert(newProductInsert);

update(sql: String)

Description: Updates data in the specified database using an SQL update statement and returns the number of affected rows. It's used for modifying existing data records in a database.

Parameters:

  • sql (type: String): The SQL update statement.

Returns: An Integer representing the number of rows affected by the update operation.

Example: Update the status of an order in the orders database.

Use Case: Common in order management systems to reflect the order's status changes.

var updateOrderStatus = "UPDATE orders SET status = 'Shipped' WHERE order_id = 1005";
var updatedRowCount = DbUtil.getDbExecutorByName("Sample Database (MySQL)").update(updateOrderStatus);

delete(sql: String)

Description: Deletes data from the specified database using an SQL delete statement and returns the number of affected rows. It's used for removing data records from a database.

Parameters:

  • sql (type: String): The SQL delete statement.

Returns: An Integer representing the number of rows affected by the delete operation.

Example: Delete expired tasks from the tasks database.

Use Case: Applicable in task management systems to clean up and remove completed or outdated tasks.

var deleteExpiredTasks = "DELETE FROM tasks WHERE status = 'Expired'";
var deletedRowCount = DbUtil.getDbExecutorByName("Sample Database (MySQL)").delete(deleteExpiredTasks);

Last updated