Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to take method name in sql string

 
kajal sharma
Greenhorn
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+"()";
System.out.println(one);
System.out.println(two);
if(one.equals("") | | two.equals("")){}else{
String si="INSERT INTO random " +"(random_no,outcomecode)"+
"VALUES(\'"+one+"\',\'"+two+"\')";
System.out.println(si);
rows = myStatement.executeUpdate(si);
System.out.println(rows);}}
please help me
kajal_sharma

 
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:
mth.invoke(obj);

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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic