You will want to connect to your database for two reasons:
- To retrieve data from the database while generating data using GenRocket Runtime using generators such as ListQueryGen, QueryBeforeLoop etc.
- To populate tables in the database using receivers like GenericSQLInsertReceiver, MySQLInsertReceiver etc.
To use these Generators and Receivers, GenRocket leverages JDBC.
What is JDBC?
JDBC stands for Java™ database connectivity (JDBC) and is the JavaSoft specification of a standard application programming interface (API) that allows Java programs to access database management systems. GenRocket can connect to any database that supports JDBC.
Step 1: Create a JDBC Configuration Properties File
For GenRocket to connect to a database via JDBC you will need to create a config.properties file. GenRocket can connect a SQL database via JDBC by reading a JDBC properties file that contains the following information.
- driver - Defines the name of the specific database driver to use to access the given database. The actual driver file must be placed in the $GEN_ROCKET_HOME/lib folder on your local computer.
- user - Defines the Username used to access the database.
- password - Defines the password used to access the database.
- url - Defines the specific URL to access the database.
- batchCount - Defines the number of SQL statements that are batched together and sent to the database for execution. This property is only mandatory for GenRocket Receivers.
driver=<JDBCDriverClassName> user=<userToConnectToDatabase> password=<passwordToConnectToDatabase> url=<URL>/<databaseName> batchCount=1000
Example config.properties for MySQL database
GenRocket will read this config file and connect to employee database on localhost using username root and password root. If you are connecting to a different type of database your configuration file will look different.
driver=com.mysql.jdbc.Driver user=root password=root url=jdbc:mysql://localhost:3306/employee?rewriteBatchedStatements=true batchCount=1000
Step 2: Add a New Resource Named resource.jdbc.directory and Update its Value within the GenRocket Web Platform
- Log into the GenRocket web platform.
- In the Resources pane, click on the Edit (Pencil) icon.
- Set the value of resource variable to the config file name with the full file path of the directory/folder that contains the config.properties file.
- For example, say you have added config.properties at location /home/user/Desktop/. Then the resource value will be /home/user/Desktop.
- In this example, the config.properties file has been stored here: C:\Users\April\dev.
- Enter the path as the value and click Save .
- The updated value for the resource.jdbc.directory resource in this image points to the dev folder.
Important: After adding the new resource with its value, you will need to update your Profile (in ~/.genrocket folder) on your system. GenRocket will read this resource value from your profile on your system to make a connection with the database.
To learn how to do this, please see one of the following pages:
- Windows: How do I download and setup my profile.grp?
- OSX/Linux: How do I download and set up my Profile.grp?
Step 3: Download and Place the JDBC connector Jar file into the $GENROCKET_HOME/lib/ folder
- Download and place the appropriate JDBC connector Jar file in your genrocket-3.5.6/lib folder.
- Links are provided below for the following: MySQL, Oracle, MS SQL.
Note: It is OK if your version doesn't match the one above -- still just place the MySQL connector in the lib directory.
Microsoft provides the JDBC driver for free. You can download the one that works for your version of SQL Server from one of these links:
- Microsoft JDBC Driver 6.2 for SQL Server
- Microsoft JDBC Driver 6.0 for SQL Server
- Microsoft JDBC Driver 4.2 for SQL Server
- Microsoft JDBC Driver 4.1 for SQL Server
- Microsoft JDBC Driver 4.0 for SQL Server
For information about system requirements and detailed information about each of the drivers listed above, please go to https://docs.microsoft.com/en-us/sql/connect/jdbc/system-requirements-for-the-jdbc-driver.
Step 4: Reference the JDBC Resource in your Generators or Receivers
When you create a Scenario that has a Generator or Receiver that references your jdbc.config resource it will connect directly to your database.