jeudi 28 janvier 2016

How to reflectively invoke PrepareStatement.set

I'm trying to reflectively invoke the setXxx method of com.mysql.jdbc.PreparedStatement using the following code.

String type = getJavaTypeFromMySqlType(); // this is "String"
Class clazz = type.getClass(); // This is java.lang.String
Method method = com.mysql.jdbc.PreparedStatement.class.getDeclaredMethod("set"+type, int.class, clazz);
method.invoke(cnt, clazz.cast(pair.getValue())); 

The last line throws

java reflection object is not an instance of declaring class

What am I missing here?





Aucun commentaire:

Enregistrer un commentaire