MrScf
MrScf

Reputation: 2497

Java Reflection - Method not found

I am trying to find the method of a PreparedStatement (ps):

Method method = ps.getClass().getMethod("setLong", int.class, Class.forName("java.lang.Long"));
method.setAccessible(true);
method.invoke(ps, fieldIndex, value);

but it isn't found. I have to use Class.forName("java.lang.Long") instead of Long.class.

For String it works:

Method method = ps.getClass().getMethod("setString", int.class, Class.forName("java.lang.String"));
method.setAccessible(true);
method.invoke(ps, fieldIndex, value);

What am I doing wrong? Any idea? Is the namespace of Long wrong?

Upvotes: 1

Views: 859

Answers (1)

assylias
assylias

Reputation: 328619

The second argument is a long, not a Long:

Method method = ps.getClass().getMethod("setLong", int.class, long.class);

Also, for a String, you don't need to call Class.forName("java.lang.String"): String.class would work as well.

But as commented, if you already have a PreparedStatement instace, you could simply call:

ps.setLong(fieldIndex, value);

Upvotes: 10

Related Questions