JMeter JDBC request learning for performance tools

Time:2021-10-21

background
“A peak forms on the side of a mountain, and the distance is different. You don’t know the true face of Lushan Mountain, but you only live in this mountain.” you can reach “thousands of mountains in the same month, all households in spring. Thousands of rivers have water, thousands of rivers have moon, and thousands of miles have no clouds.”

Principle of JDBC connection to database
Concept: Java database connectivity, Java database connection, Java language operation database, JDBC essence: in fact, it is a set of rules defined by the official (Sun company) to operate all relational databases, that is, interfaces. Each database manufacturer implements this interface and provides database driver jar package. We can use this set of interfaces (JDBC) to program, and the real executed code is to drive the implementation classes in the jar package.

Java link method review
Operation steps
Import the driver jar package MySQL connector Java * *. Jar package

Register driver

Get database connection object connection

Define SQL

Gets the statement object that executes the SQL statement

Execute SQL and accept the returned results

Processing results

Release resources

Reference code:
public static void main(String[] args) throws ClassNotFoundException, SQLException {

//1. Import the driver jar package

//2. Register driver

Class.forName(“com.mysql.cj.jdbc.Driver”);

//3. Get database connection object

Connection conn = DriverManager.getConnection(“jdbc:mysql://localhost:3306/7d?useUnicode=true&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC”, “root”, “123456”);

//4. Define SQL statements

String sql = “select * from user_table”;

//5. Get the object statement executing SQL

Statement stmt = conn.createStatement();

//6. Execute SQL

ResultSet resultSet = stmt.executeQuery(sql);

while (resultSet.next()) {

System.out.println (“user:” + resultset.getstring (“create_user”) + “password:” + resultset.getstring (“pass_word”));

}

//7. Treatment results

//8. Release resources

stmt.close();

conn.close();

}

Explanation:
Drivermanager: drive management objects

Static void registerdriver (driver driver): register with the given driver drivermanager.

Write code using: class. Forname (“com. Mysql. CJ. JDBC. Driver”);

By looking at the source code, it is found that there are static code blocks in the com.mysql.jdbc.driver class

Register driver: tell the program which database driver jar to use

Get database connection:

Method: static connection getconnection (string URL, string user, string password)

URL: Specifies the path of the connection

Syntax: JDBC: mysql://ip Address (domain name): port number / database name

Example: JDBC: mysql://localhost:3306/7d

Details: if the local MySQL server is connected and the default port of MySQL service is 3306, the URL can be abbreviated as JDBC: MySQL: / / / database name

User: user name

Password: password

Connection: database connection object

Get the object executing SQL

Statement createStatement()

PreparedStatement prepareStatement(String sql)
Management services:

Start transaction: setautocommit (Boolean autocommit): call this method to set the parameter to false, that is, start the transaction

Commit transaction: commit()

Rollback transaction: rollback()

Statement: object to execute SQL

Execute SQL

Boolean execute (string SQL): arbitrary SQL can be executed

Int executeupdate (string SQL): execute DML (insert, update, delete) statements and DDL (create, alter, drop) statements. Return value: the number of rows affected. You can judge whether the DML statement is executed successfully by the number of rows affected. If the return value is > 0, the execution is successful, otherwise, it fails.

Resultset executeQuery (string SQL): execute DQL (select) statements

Add a record to the account table

Account table modification record

Delete a record from the account table

Resultset: result set object that encapsulates query results

Boolean next(): move the cursor down one line to determine whether the current line is the end of the last line (whether there is data). If yes, it returns false. If not, it returns true

Getxxx (parameter): get data XXX: represents the data type, such as int getint(), string getstring()

Int: represents the number of the column, starting from 1, such as getString (1)

String: represents the name of the column. For example: getdouble (“balance”)

Use steps:

Move cursor down one line

Determine whether there is data

get data

//Loop to determine whether the cursor is at the end of the last line.

while(rs.next()){

//Get data

//6.2 data acquisition

int id = rs.getInt(1);

String name = rs.getString(“name”);

double balance = rs.getDouble(3);

System.out.println(id + “—>” + name + “—>” + balance);

}

A brief introduction to JDBC linking in JMeter
New test plan

Add JDBC connection configuration

JDBC Request

Call test plan
Select MySQL driver
JMeter JDBC request learning for performance tools
Open JDBC connection configuration
JMeter JDBC request learning for performance tools
JMeter JDBC request learning for performance tools
Description of important parameters

Variable name: the name of the database connection pool. We can have multiple JDBC connection configurations, each of which can have a different name. In the JDBC request, you can use this name to select an appropriate connection pool.

Database URL: database URL, JDBC: MySQL: / / host IP or machine name: the port number / database name that MySQL listens to, such as JDBC: mysql://localhost:3306/7d

Jdbc driver class: jdbc driver
Username: user name of database login
Passwrod: password for database login
Create a new JDBC request
JMeter JDBC request learning for performance tools

Enter query statement
JMeter JDBC request learning for performance tools

Description of important parameters:

Variable name: the name of the database connection pool, which should be consistent with the variable name bound pool name of JDBC connection configuration

Query: filled in SQL statement

Parameter value: parameter value

Parameter types: parameter types. Refer to Javadoc for java.sql.types

Variable names: the name of the variable that holds the result returned by the SQL statement

Result variable name: create an object variable to save all returned results

Query timeout: query timeout

Handle result set: defines how to handle the results returned by the call statements statement

Add result view tree
The execution result is displayed as:

JMeter JDBC request learning for performance tools

summary
The above is the simplest operation. I hope to review it for you;
JMeter JDBC request learning for performance tools