简体   繁体   English

如何通过从查询获得的结果中以字符串形式获取方法名称来从另一个类中调用Java中的方法

[英]how to invoke a method in java from another class by obtaining the method name as a string from the result obtained from a query

I am trying to invoke method from another class by specifying the method name. 我试图通过指定方法名称从另一个类调用方法。 The method name is received from the result obtained from a query... 从查询获得的结果中接收方法名称。

Class:VAL 类别:VAL

ps1=connection.prepareStatement("select method_name from validation where tablename=? and type_name=?");
ps1.setString(1, "table_name");
ps1.setString(2, "fieldtype");
ResultSet rs1 = null;
rs1=ps1.executeQuery();
while(rs1.next())
{
methname=rs1.getString(1);
}
rs1.close();
return status;

Now I should use this method name suppose if its methname="Alphanumeric" I should call this method written in class Special_validation from class VAL. 现在,我应该使用此方法名称,前提是它的methname =“ Alphanumeric”应该调用从VAL类以Special_validation类编写的此方法。

Class:Special_validation 类别:特殊验证

Class Special_validation
{
public boolean Alphanumeric()
{
------
------
}
}

You could use java reflection. 您可以使用Java反射。

A call to Class.getDeclaredMethod() would return a Method. 调用Class.getDeclaredMethod()将返回一个Method。 You could use Method.invoke() to call this method. 您可以使用Method.invoke()来调用此方法。

You might also want to get function prototype, not just the name, before calling getDeclaredMethod() 您可能还想在调用getDeclaredMethod()之前获取函数原型,而不仅仅是名称。

Reference 参考

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM