LR接口测试---Java Vuser之增删改查

 1 import lrapi.lr;
 2 
 3 import java.sql.Connection;
 4 import java.sql.DriverManager;
 5 import java.sql.PreparedStatement;
 6 import java.sql.ResultSet;
 7 
 8 public class Actions {
 9     //定义用户名,密码,连接的URL
10     String username = "root";
11     String password = "123456";
12     String url = "jdbc:mysql://127.0.0.1:3306/hk";
13     //String sql = "select * from teach where id = ?";
14     String sql = "update teach set name = ? where id = 2";
15     Connection conn = null;
16     PreparedStatement ps;
17     ResultSet set = null;
18     //所有的并发只执行一次的操作,放在static代码块里
19     static{
20 
21     //1注册mysql驱动
22         try{
23         Class.forName("com.mysql.jdbc.Driver");
24         }catch(ClassNotFoundException e){
25             e.printStackTrace();
26         }
27     }
28     public int init() throws Throwable {
29 
30         //2连接mysql,导入java.sql.Connection
31         conn = DriverManager.getConnection(url,username,password);
32         //3获取sql执行器的类,导入java.sql.preparedstatement
33         ps = conn.prepareStatement(sql);
34         return 0;
35     }// end of init
36 
37     public int action() throws Throwable {
38 
39         //字符串转换成整形
40         //Integer.parseInt("<id>");
41         //对sql语句中的?做赋值的操作,为了做参数化
42         //ps.setInt(1, Integer.parseInt("<id>"));
43         ps.setInt(1,123456);//详细参数详见页底图片
44 
45 
46     lr.start_transaction("shiwu");//事务开始
47 
48 
49         //4执行sql语句,获取到执行结果对象ResultSet(导入)
50         //set = ps.executeQuery();//executeQuery方法只适用于查询操作
51 
52         //如果是insert,update,delete。使用executeUpdate方法。定义的rows为影响多少行,如果大于0事务成功
53         int rows = ps.executeUpdate();
54          if(rows > 0 ){
55              lr.end_transaction("shiwu", lr.PASS);
56          }else{
57              lr.end_transaction("shiwu", lr.FAIL);
58          }
59         
60         //if(set.next() == true ){
61                     
62           //  lr.end_transaction("shiwu", lr.PASS);
63 
64         //}else{
65                     
66           //  lr.end_transaction("shiwu", lr.FAIL);
67 
68          // }
69         //查询时打印获取到的数据
70 //        while(set.next()){
71 //            int id = set.getInt("id");
72 //            System.out.println("id"+id);
73 //        }
74         return 0;
75     }// end of action
76 
77     public int end() throws Throwable {
78         //关闭掉ResultSet,PreparedStatement,Connection
79         //set.close();//更新,删除和插入用不到set.close()把它去掉
80         ps.close();
81         conn.close();
82         return 0;
83     }// end of end
84     
85 /*    //程序去执行的入口,在LR中不用写
86     public static void main(String[] args) throws Throwable{
87         Actions actions = new Actions();
88         actions.init();
89         actions.action();
90         actions.end();
91     }
92 */
93 
94 }

以上为java vuser增删改查的脚本,其中明确写出来 查和改 的示例。

注释:

1、运行时 设置Run-time Settings看下是否存在java的classpath

2、设置运行所需要的JDBC包并添加进来

3、附一张截图说明,如下图所示:

posted @ 2016-04-07 00:34  韩凯1990  阅读(510)  评论(0编辑  收藏  举报