LR十四:Update类型_JDBC的方法_JAVA方法_Loadrunner脚本

java vuser   JDBC 参数化的方法

如果不进行参数化 直接把32  33行去掉 ,sql 值写到valuers  中就行了

下面这是 insert,delete,update  三种方法的sql 方法模板  

注:数据库增删改 都是 第37行  int rows = ps.executeUpdate();            Update()方法,本来就都是Update操作

步骤:
1.注册驱动
2.通过用户名,密码,数据库url等信息,获取jdbc链接Connection
3.通过jdbc链接,对sql语句进行预编译,得到PreparedStatement
4.对sql语句进行传参数
5.执行sql语句
6.获取数据
    insert、update、delete返回的是操作的是数据行数
    Select返回的ResultSet对象,可以获取对应的列值
7.关闭链接
   非空的情况下,关闭Connection
   非空的情况下,关闭PreparedStatement

 这是在java 中模拟执行

 1 package com.test;
 2 //包名
 3 
 4 import java.sql.Connection;
 5 import java.sql.DriverManager;
 6 import java.sql.PreparedStatement;
 7 
 8 
 9 public class Jdbc_Update {
10     String user = "root";
11     String password = "123456";
12     String url = "jdbc:mysql://localhost:3306/oa?useUnicode=true&characterEncoding=utf8";
13     String sql = "insert into itcast_role (name, description) values (?,?)";
14     //定义成全局变量
15     PreparedStatement ps;
16     Connection conn;
17     
18     public int init() throws Throwable {
19         //1.注册mysql驱动
20         Class.forName("com.mysql.jdbc.Driver");
21         //2.通过url,user,password建立mysql连接,返回Connection的对象
22         //第三个getConnection - [记住的]
23         conn = DriverManager.getConnection(url, user, password);
24         //3.对sql进行预编译,返回一个编译过的sql对象PreparedStatement -[记住的]
25         ps = conn.prepareStatement(sql);
26         return 0;
27     }//end of init
28 
29 
30     public int action() throws Throwable {
31         //4.执行前对不确定的sql数据进行赋值
32         ps.setString(1, "产品");
33         ps.setString(2, "设计产品");
34         
35         
36         //5.返回受影响的行数
37         int rows = ps.executeUpdate();
38         System.out.println("受影响的行数"+ rows);
39         return 0;
40     }//end of action
41 
42 
43     public int end() throws Throwable {
44         //6.关闭连接
45         conn.close();
46         return 0;
47     }//end of end    
48     
49 
50     public static void main(String[] args) throws Throwable {
51         // 模拟lr一下执行
52         Jdbc_Update test = new Jdbc_Update();
53         test.init();
54         test.action();
55         test.end();
56 
57 
58     }
59 
60 }
java_jdbc方法

这是在loadrunner中的模拟执行

注意:如果要参数化 ,使用的是< > 不是{ }

 1 /*
 2  * LoadRunner Java script. (Build: _build_number_)
 3  * 
 4  * Script Description: 
 5  *                     
 6  */
 7 
 8 import lrapi.lr;
 9 import java.sql.Connection;
10 import java.sql.DriverManager;
11 import java.sql.PreparedStatement;
12 
13 public class Actions
14 {
15 
16 
17     String user = "root";
18     String password = "123456";
19     String url = "jdbc:mysql://localhost:3306/oa?useUnicode=true&characterEncoding=utf8";
20     String sql = "insert into itcast_role (name, description) values (?,?)";
21     //定义成全局变量
22     PreparedStatement ps;
23     Connection conn;
24     
25     public int init() throws Throwable {
26         //1.注册mysql驱动
27         Class.forName("com.mysql.jdbc.Driver");
28         //2.通过url,user,password建立mysql连接,返回Connection的对象
29         //第三个getConnection - 记住的
30         conn = DriverManager.getConnection(url, user, password);
31         //3.对sql进行预编译,返回一个编译过的sql对象PreparedStatement -记住的
32         ps = conn.prepareStatement(sql);
33         return 0;
34     }//end of init
35 
36 
37     public int action() throws Throwable {
38         //---------事务action() --判断开始---------
39             
40         lr.start_transaction("insert-1");
41 
42         ps.setString(1, "<dataP>");
43         ps.setString(2, "<dataP>");
44 
45         //5.返回受影响的行数
46         int rows = ps.executeUpdate();
47         //---------注释掉打印免得浪费压测资源---------
48 //         System.out.println("受影响的行数"+ rows);
49 
50         //因为上面一行受影响的行数,返回的是布尔函数 是就True 不是就false   “==1表示相等”所以这样判断就可以了
51         if(rows == 1){
52                     
53             lr.end_transaction("insert-1", lr.PASS);
54 
55         }else{
56                     
57             lr.end_transaction("insert-1", lr.FAIL);
58 
59         }
60 
61 
62         return 0;
63     }//end of action
64 
65 
66     public int end() throws Throwable {
67         //6.关闭连接
68         conn.close();
69         return 0;
70     }//end of end    
71 }
loadrunner_jdbc

这是loadrunner的运行日志

 1 Virtual User Script started at : 2019-03-30 17:44:02
 2 Starting action vuser_init.
 3 Ending action vuser_init.
 4 Running Vuser...
 5 Starting iteration 1.
 6 Starting action Actions.
 7 Notify: Transaction "insert-1" started.
 8 Notify: Transaction "insert-1" ended with "Pass" status (Duration: 0.0214).
 9 Ending action Actions.
10 Ending iteration 1.
11 Ending Vuser...
12 Starting action vuser_end.
13 Ending action vuser_end.
14 Vuser Terminated.
log

 

附 参数化

 

 

——> query 查询数据库压测脚本    https://www.cnblogs.com/zhenyu1/p/10628822.html 

 

posted @ 2019-03-29 12:34  测试艺术家  阅读(427)  评论(0编辑  收藏  举报