This week's book giveaway is in the JDBC forum.
We're giving away four copies of Make it so: Java DB Connections & Transactions and have Marcho Behler on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes How to take method name in sql string Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Make it so: Java DB Connections & Transactions this week in the JDBC forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to take method name in sql string " Watch "How to take method name in sql string " New topic

How to take method name in sql string

kajal sharma

Joined: Oct 04, 2000
Posts: 22
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

Joined: Feb 20, 2001
Posts: 3695

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.
I agree. Here's the link:
subject: How to take method name in sql string
It's not a secret anymore!