Answered question

I would like to run a SQL statement from a Javascript but I don't know how to do it.

Basically, I need to run a SQL statement during my load testing to get fresh data that may be used during the test. I can't run my SQL query before the test but only during the test. So, I will have to do it from a Javascript. How can I achieve that?

Since NeoLoad 5.0, an advanced action can be used to execute SQL statement from NeoLoad. It's not necessary anymore to use a Javascript. 

However, in order to use custom action, your license should have the Custom Action module.

Please check the Neotys Labs here for more information

Did you find this useful ?

No (1)

Yes (0)


0% of users found this answer useful

Other answers

NeoLoad has a built-in variable where you can run SQL requests against different type of databases such as MySQL, SQL server, Oracle, DB2 and PostgreSQL.

This variable will be evaluated at the beginning of the script only. So, if you have to run other SQL queries, you will have to do it from a Javascript using JAVA code.

As an example, Neotys has created JAVA code that will allow you to run such SQL queries from a Javascript.

First, you have to download the nl-sql.jar.pdf (use the "Save link as" feature from your browser) copy the nl-sql.jar.pdf (rename it nl-sql.jar) into your <neoload_project>\lib\jslib folder.

Then, create your Javascript in NeoLoad.

This is an example of connection:

var connection = new Packages.SQLConnection("mysql");
connection.connect("localhost", 3306, "mysql", "root", "admin");

The different types of SQL servers that can be used are : mysql, oracle, db2, postgresql, mssql.
The connection parameters are in this order: name of the server (or IP address), port, database name, username and password.

Then, to select multiple results:
var results = connection.getResults("SELECT * FROM user");
// we iterate on each item returned by the SQL statement

for (var i = 0 ; i < results.size() ; i++) {
  var item = results.get(i);
// Each column of the database can be accessed through its name:
  logger.debug("name = " + item.get("name"));

To select a single result:
var result = connection.getSingleResult("SELECT * FROM user WHERE name = 'user1'");
logger.debug("name = " + result.get("name")); // This will display: user1

At the end of the Javascript we have to close the connection:

For more information, see “NeoLoad Javascript API” under “Appendixes” section in NeoLoad documentation.