在Java/beanshell代码中操作JMeter JDBC连接字段
这是一个后续行动。如何在DataSourceElement类中使用getPassword()或getUsername等方法?我想从JDBC连接配置中获取并修改用户名和密码(以及其他内容) 我在那篇文章中使用了相同的代码,但使用的是beanshell采样器(最终将在预处理器元素中使用)。我认为问题在于我需要一个DataSourceElement对象,并且只定义了一个连接对象。这就是我被困的地方 我的代码:在Java/beanshell代码中操作JMeter JDBC连接字段,java,jdbc,jmeter,beanshell,Java,Jdbc,Jmeter,Beanshell,这是一个后续行动。如何在DataSourceElement类中使用getPassword()或getUsername等方法?我想从JDBC连接配置中获取并修改用户名和密码(以及其他内容) 我在那篇文章中使用了相同的代码,但使用的是beanshell采样器(最终将在预处理器元素中使用)。我认为问题在于我需要一个DataSourceElement对象,并且只定义了一个连接对象。这就是我被困的地方 我的代码: import java.sql.Connection; import org.apache.
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返回以下内容:
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'
控制台输出返回连接对象,然后在错误处停止:
beanshell script beginning
org.postgresql.jdbc4.Jdbc4Connection@7849e2a7
可以使用类获取一些连接信息,如:
import java.sql.Connection;
import org.apache.jmeter.protocol.jdbc.config.DataSourceElement;
import java.sql.DatabaseMetaData;
Connection con = DataSourceElement.getConnection("jdbcconfig");
DatabaseMetaData meta = con.getMetaData();
String conusername = meta.getUserName();
print(conusername);
但是我不认为你可以通过这种方式获得密码,因为这种能力会带来巨大的安全风险
顺便说一下,通过将Beanshell代码放入try/catch块,您可以在jmeter.log文件中获得更友好的异常消息,如:
try {
//your code here
}
catch (Throwable ex) {
log.error("Something wrong", ex);
throw ex;
}
有关使用Beanshell测试元素中的JMeter和JavaAPI的更多信息,请参阅