• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to take method name in sql string

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic