Skip to content

qie.getDbConnection

Signature: qie.getDbConnection(connectionName)

Returns: java.sql.Connection dbConnection - an open database connection to connectionName

Returns an open database connection.

Note

The database connection remains open and in use until connection.close() is called. Failure to close the connections will exhaust the database connection pool which can cause exceptions. Using a try/finally block is recommended for ensuring the connection is always closed after use.

Parameters

Type Name Description Default
String connectionName the name of the database connection

Example

var connection = null;
try {
   // Establish DB connection
   connection = qie.getDbConnection('EMR Database');
   // Start a transaction
   connection.setAutoCommit(false); // Start the transaction
   // Execute the insert query
   var query = "INSERT INTO test_table (`item`, `description`) " +
      "VALUES ('orchid', 'Howards Dream orchid is the best orchid in the world.')";
   // Create a Statement object
   var statement = connection.createStatement();
   // Execute the query
   statement.executeUpdate(query); // Use executeUpdate with a Statement object
   // Commit the transaction if no errors occurred
   connection.commit();
} catch (e) {
   // Handle any errors that occur during the transaction
   if (connection != null) {
      try {
         // Rollback the transaction if something goes wrong
         connection.rollback();
      } catch (rollbackError) {
         // Handle any errors during rollback (e.g., log them)
         qie.error("Rollback failed: " + rollbackError.message);
      }
   }
   // Log the error that occurred
   qie.error("Transaction failed: " + e.message);
} finally {
   // Ensure the connection is closed properly
   if (connection != null) {
      connection.close();
   }
}