這是Using a JMeter JDBC connection in Java code的後續操作。如何在DataSourceElement類中使用諸如getPassword()或getUsername之類的方法?我想要從JDBC連接配置配置中獲取並修改用戶名和密碼(等等)。在Java/beanshell代碼中操縱JMeter JDBC連接字段
我在那篇文章中使用了相同的代碼,但是在一個beanshell取樣器中(它最終將用於預處理器元素中)。我認爲問題是我需要一個DataSourceElement對象,並且只定義了一個Connection對象。那是我卡住的地方。
我的代碼:
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
[email protected]