Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to take method name in sql string  RSS feed

kajal sharma
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have class object rand and it has got method getRn1() getRn2() up 10 so I am using loop but it is inserting name of method as string rather then value
for(int i=1;i<11;i++){
String one="rand.getRn"+i+"()";
String two="rand.getOc"+i+"()";
if(one.equals("") | | two.equals("")){}else{
String si="INSERT INTO random " +"(random_no,outcomecode)"+
rows = myStatement.executeUpdate(si);
please help me

Mike Curwen
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you say This simply inserts the contents of the string 'one' and 'two' into String si, but does not actually do the method call. And thank goodness for that.

By the way, this is a really bad way of designing your object. It should be something like the rand object having a method called getRand, that takes as a parameter the numbers 1-10.

And if you really want to do it this way, I *think* there is a way to do it. I know how to do this with VB, but I've looked in the API and check this out:
Class obj = Class.forName("rand");
will return a class object. Given that object, then use:
Method mth = obj.getDeclaredMethod(one);
and finally use:

By the way, I've never used that code, and I don't totally understand the invoke method's description for its first object parameter. If you make the getRndx methods static, you can leave it like this: mth.invoke();

But like I said, try this whole thing a different way.
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!