jmeter中vars.putObject的使用:可传递int变量

本文转自:http://stackoverflow.com/questions/20866023/jmeter-var-putobject-variable-use-in-other-jdbc-request-but-it-is-ignored

Test Plan Thread Group JDBC Connection JSR223 Sampler JDBC Request Result Tree

Put codes in the JSR223 Sampler:

int id = Integer.parseInt( vars.get("party_id") ); vars.putObject("id", new Integer(id));

Then use id in JDBC Request:

    select * from table where column = ? 
    Parameter Value: ${id}
    parameter Type: NUMBER

It seems the JDBC request is ignored and no output shown in the result tree.

Please help to look into the issue...

Thanks in advance

share improve this question
 

2 Answers

User Defined Variable stores value of String but not Integer. I wonder if there is any error/warning in JMeter console (log).

Cast you integer to String to put it into UDV. enter image description here

Just specify ${UDV} if you wanted it to be an integer. Add quotes ('$UDV') if you want it as varchar in SQL query enter image description here

enter image description here

share improve this answer
 
 
I also add debug sampler into the test plan, variable "id" has the value shown in the result tree. Observe the issue from the log file "jmeter.threads.JMeterThread: Error while processing sampler 'try integer' : java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String". Do you have any idea how to get the integer variable used in the other sampler? –   ming  Jan 2 '14 at 5:23
 
When you use putObject method just cast your integer into String using toString() method.vars.putObject("id", id.toString()); –   olyv  Jan 2 '14 at 6:58
 
If i use string it works fine for my select statement where i can change the type to varchar. I have one insert statement for one jdbc request required number parameter. When i pass "${id}" not working if cast it to String.–   ming  Jan 2 '14 at 7:51
 
I'm not sure I've got your option. If you have select query you write it as select * from table where condition = ${id}. Supposing your variable has value 5 (it is string, because user defined variable stores only string) next query will be executed select * from table where condition = 5. If you need varchar in the SQL query you write select * from table where condition = '${id}' and next will be executed select * from table where condition = '5'. JMeter HTTP or JDBC sampler treat ${variable} the next way: just get the text from variable and put it into sampler body. Is it the answer? –   olyv Jan 2 '14 at 8:34
 
Thanks for the details. Method vars.putObject can store any type of object for user defined variable, for example integer. I wonder if there is some way to get the variable whose type is not string. I have one insert query [insert into table (id number) values (${id})] required integer in one jdbc request using the variable storing integer defined in JSR223 sampler. As you have mentioned, ${id} type is string, any way to convert to integer? –   ming  Jan 2 '14 at 10:30 

Have reproduced your problem, it looks like that JDBC Sampler doesn't accept integers, only Strings.

I'm getting following error in jmeter.log, guess you too:

2014/01/04 11:30:14 ERROR - jmeter.threads.JMeterThread: Error while processing sampler 'JDBC Request' : java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String

So your cast to integer is absolutely not required. Try changing your JSR223 code to following:

vars.put("id", vars.get("party_id")); 

It should work fine.

The bit, which tells MySQL which datatype bind the variable to is ParameterType stanza. You need to specify paratemer type there, not in JSR223

share improve this answer

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值