background
When viewed from a horizontal perspective, a mountain becomes a peak on its side, with different distances and heights. I do not know the true face of Lushan Mountain, but I am in it. Insist on continuous learning can reach “thousands of mountains in the same month, ten thousand households are all spring. Thousands of rivers have water thousands of rivers on, thousands of miles without clouds thousands of days.”
JDBC Database connection principle
Java DataBase Connectivity Java DataBase Connectivity, Java language to operate the DataBase,JDBC essence: in fact, the official (Sun Company) defined a set of rules to operate all relational databases, namely the interface. Each database vendor to implement this interface, provide the database driver JAR package. We can program using this set of interfaces (JDBC), and the actual code that executes is the implementation class in the driver JAR package.
Java link method review
steps
- Import the driver JAR package mysql-connector-java**. Jar package
- Registration drive
- Gets the database Connection object Connection
- Define the SQL
- Gets the Statement object where the SQL Statement is executed
- Execute the SQL and accept the return results
- The processing results
- Release resources
Reference code:
public static void main(String[] args) throws ClassNotFoundException, SQLException {
Import the driver JAR package
//2. Register the driver
Class.forName("com.mysql.cj.jdbc.Driver");
//3. Obtain the database connection object
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/7d? useUnicode=true&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UT C", "root", "123456");
//4. Define SQL statements
String sql = "select * from user_table";
//5. Obtain Statement
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. Processing result
//8. Release resources
stmt.close();
conn.close();
}
Explanation:
- DriverManager: Driver managed object
Static void registerDriver(Driver Driver) : Registers with the given Driver DriverManager.
- Class. ForName (” com.mysql.cj.jdbc.driver “);
- Static code blocks are found in the com.mysql.jdbc.driver class
- Register the driver: tells the program which database driver JAR to use
- Get database connection:
Static Connection getConnection(String URL, String user, String password)
- Url: Specifies the connection path
- JDBC :mysql:// IP address (domain name): port number/database name
- Example: JDBC: mysql: / / localhost: 3306/7 d
- Details: If you are connecting to a native mysql server and the default mysql service port is 3306, the URL can be shortened to: JDBC :mysql:/// database name
- User: indicates the user name
- Password: password
- Connection: database Connection object
Gets the object that executes the SQL
- Statement createStatement()
- PreparedStatement prepareStatement(String SQL) Manage transaction:
- Start a transaction: setAutoCommit(Boolean autoCommit) : Call this method and set the parameter to false, that is, start a transaction
- Commit transaction: commit()
- Rollback transaction: rollback()
- Statement: Object to execute SQL
Execute SQL
- Boolean execute(String SQL) : Can execute any SQL query
- Int executeUpdate(String SQL) : execute DML (INSERT, UPDATE, delete) statements, DDL(create, alter, drop) statements The number of affected rows can be used to determine whether the DML statement is successfully executed if the returned value is greater than 0. Otherwise, the DML statement fails.
- ResultSet executeQuery(String SQL) : Executes the DQL (SELECT) statement
- Add a record to the account table
- Account table modification records
- Delete a record from the account table
- ResultSet: a ResultSet object that encapsulates query results
- Boolean next(): Moves the cursor down one line to determine if the current line is the end of the last line (if there is data), return false if so, and true if not
- GetXxx (parameter): indicates the data type. For example, int getInt(), String getString()
- Int: indicates the column number, starting with 1. For example, getString(1)
- String: indicates the column name. Such as: the getDouble (” balance “)
- Use steps:
- The cursor moves down one row
- Determine if there is data
- To get the data
// Loop to determine if the cursor is the end of the last line.
while(rs.next()){
// Get data
6.2 Obtaining Data
int id = rs.getInt(1);
String name = rs.getString("name");
double balance = rs.getDouble(3);
System.out.println(id + "--->" + name + "--->" + balance);
}
This section describes the JDBC connection mode in Jmeter
- Creating a Test plan
- Added the JDBC Connection Configuration
- JDBC Request
A Test Plan
Select mysql driver
The JDBC Connection Configuration is enabled
Fill in the
Key Parameters
- Variable Name: indicates the Name of the database connection pool. You can have multiple JDBC Connection configurations, each with a different Name. You can use this Name to select an appropriate connection pool in the JDBC Request.
- The Database URL: Database URL, JDBC: mysql: / / host IP or machine name: mysql listening port/Database name, such as: JDBC: mysql: / / localhost: 3306/7 d
- JDBC Driver class: indicates the JDBC Driver
- Username: indicates the username for logging in to the database
- Passwrod: indicates the password for logging in to the database
The new JDBC Request
Input query statement
Important parameters:
- Variable Name: Specifies the Name of the database Connection Pool, which must be consistent with the Variable Name Bound Pool Name of JDBC Connection Configuration
- Query: indicates the ENTERED SQL statement
- Parameter valus: Parameter value
- Parameter types: indicates the Parameter types. For details, see Javadoc for java.sql. types
- Variable names: Variable names that store the results returned by SQL statements
- Result Variable name: Creates an object variable that holds all returned results
- Query timeout: indicates the Query timeout time
- Handle Result Set: Defines how to process the results returned by the Callable statements
Add result view tree
The command output is as follows:
conclusion
The above is the simplest operation, I hope to give you a review;