Use JDBC Drivers From Scripts

Applies to ReadyAPI 3.52, last modified on April 18, 2024

You can perform direct JDBC calls from Groovy scripts.

This topic describes how to use a Groovy script to register the JDBC driver and connect to the database.

We recommend using the JDBC Request test step and JDBC data source instead of Groovy scripting.

Where to use

There are several ways to run this script:

Example

The following code demonstrates how to register the MySQL JDBC driver and connect to the database:

Groovy

// Import the Groovy class required to work with SQL databases
import groovy.sql.Sql

// Set up database connection properties
def url = 'jdbc:mysql://mydatabase.com/DatabaseVariable' /* IMPORTANT: must start with jdbc:mysql:// */
def user = 'DatabaseUser'
def password = 'DatabasePassword'
def driver = 'com.mysql.jdbc.Driver'

// Register the MySQL JDBC driver – required for Groovy to send requests to the database
com.eviware.soapui.support.GroovyUtils.registerJdbcDriver( driver )

// Connect to the SQL instance
def sql = Sql.newInstance(url, user, password, driver)

// Use the SQL instance
// ...

// Close the SQL instance
sql.close()

Before registering the driver, you need to place its JAR file to the <ReadyAPI Installation>\bin\ext directory.

See Also

JDBC Request Test Step
JDBC Data Source
Preconfigured JDBC Drivers
Groovy Scripting Samples
Scripting

Highlight search results