用Java/beanshell代码处理JMeter JDBC连接字段 [英] Manipulating JMeter JDBC connection fields in Java/beanshell code
问题描述
这是>在Java代码中使用JMeter JDBC连接的后续操作.如何在DataSourceElement类中使用诸如getPassword()或getUsername之类的方法?我想从JDBC连接配置配置中获取用户名和密码,然后进行修改.
This is a followup to Using a JMeter JDBC connection in Java code. How do I use the methods such as getPassword() or getUsername in the DataSourceElement class? I want to get and then modify the username and password (among other things) from the JDBC Connection Configuration config.
我在那篇文章中使用了相同的代码,但是在一个beanshell采样器中(最终将在预处理器元素中使用它).我认为问题是我需要一个DataSourceElement对象,并且只定义了一个Connection对象.那就是我被困住的地方.
I'm using the same code in that post, but in a beanshell sampler (which will ultimately be used in a preprocessor element). I think the problem is that I need a DataSourceElement object and have only defined a Connection object. That is where I am stuck.
我的代码:
import java.sql.Connection;
import org.apache.jmeter.protocol.jdbc.config.DataSourceElement;
print("\nbeanshell script beginning");
try {
Connection con = DataSourceElement.getConnection("jdbcconfig");
print(con);
String conpasswd = con.getPassword();
print(conpasswd);
if(con!=null)
con.close();
}
catch (Throwable ex) {
log.error("Something went wrong: ", ex);
}
print("the end");
jmeter.log返回此:
jmeter.log returns this:
ERROR - jmeter.util.BeanShellInterpreter: Error invoking bsh method: eval Sourced file: inline evaluation of: ``import java.sql.Connection; import org.apache.jmeter.protocol.jdbc.config.DataSo . . . '' : Typed variable declaration : Error in method invocation: Method getPassword() not found in class'com.sun.proxy.$Proxy0'
然后控制台输出返回连接对象,然后在错误处停止:
And the console output returns the connection object and then stops at the error:
beanshell script beginning
org.postgresql.jdbc4.Jdbc4Connection@7849e2a7
推荐答案
使用
但是我不认为您将能够以这种方式获取密码,因为该功能会带来巨大的安全风险. But I don't think you will be able to get the password this way as the ability would cause immense security risks. 顺便说一句,通过将Beanshell代码放在try/catch块中,您可以在 jmeter.log 文件中获得更友好的异常消息: By the way, you can get more friendly exception message in jmeter.log file by surrounding your Beanshell code into try/catch block like: 请参见如何使用BeanShell:JMeter最喜欢的内置组件有关通过Beanshell测试元素使用JMeter和Java API的更多信息 See How to Use BeanShell: JMeter's Favorite Built-in Component for more information on using JMeter and Java APIs from Beanshell test elements 这篇关于用Java/beanshell代码处理JMeter JDBC连接字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!try {
//your code here
}
catch (Throwable ex) {
log.error("Something wrong", ex);
throw ex;
}