AbstractMethodError on resultset.getObject

2020-04-07 12:23发布

问题:

So I'm building a minecraft plugin, one part of the plugin grabs a bunch of block data from mysql and loads it into a cache when the server starts. I have a bit of code that runs fine in eclipse test cases. However when I load the plugin in a local minecraft server the I get the exception.

    java.lang.AbstractMethodError: Method com/mysql/jdbc/JDBC4ResultSet.getObject(Ljava/lang/String;Ljava/lang/Class;)Ljava/lang/Object; is abstract
            at com.mysql.jdbc.JDBC4ResultSet.getObject(JDBC4ResultSet.java) ~[spigot-1.8.8.jar:git-Spigot-db6de12-d3e0b6f]
            at fws.plugins.trigger.database.ModelDB.loadCollection(ModelDB.java:335) ~[?:?]
            at fws.plugins.trigger.database.ModelDB.all(ModelDB.java:295) ~[?:?]
etc...



The bit of code that is throwing the exeption.

rs.getObject( field.getName(), p.fieldType());

rs is a java.sql.ResultSet instance returned from a excuted query.
p.fieldType() just returns a Class<?>



Slightly bigger snippet... not that it really shows you anything else.

if (field.isAnnotationPresent(Persist.class)) {
    try {
        Persist p = field.getAnnotation(Persist.class);
        Object o = rs.getObject( field.getName(), p.fieldType());
        field.set(m,p.fieldType().cast(o));

    } catch (Exception e) {
        // TODO Auto-generated catch block 
        e.printStackTrace();
    } 
}

I have looked online people said to fix I need to include ojdbc6.jar and use it as my Connection Driver.
I added the file to the project structure under a folder lib, included it to my project then added it to my Build File. http://i.imgur.com/7TXLbjj.png and changed the connection driver to oracle.jdbc.OracleDriver

However im getting the same issue, seems like not a fix. Although chances are i have done it all wrong.

Can anyone help me, any insights etc?

EDIT**
from the commandline

$ java -version
java version "1.8.0_51"
Java(TM) SE Runtime Environment (build 1.8.0_51-b16)
Java HotSpot(TM) 64-Bit Server VM (build 25.51-b03, mixed mode)


from Eclipse

System.out.println(System.getProperty("java.runtime.version"));

returns 1.8.0_51-b16



But both are on the same PC, so i would expect the same values?

回答1:

The method ResultSet.getObject(String columnLabel, Class<T> type) was added in JDBC 4.1 (Java 7). It looks like you are using a JDBC 4.0 driver, and not a JDBC 4.1 (or JDBC 4.2/Java 8) driver.

You may need to update your JDBC driver (the latest for Connector/J MySQL driver is 5.1.38).



回答2:

Ok Sorry about the Question I have Rethought my approach. Instead of using reflection to set the types of the class members, I have just impletented a method in each data model class that maps ResultSet values to the current object.

@Override
public Select load(ResultSet rs) throws SQLException {
    this.id = rs.getInt("id");
    this.name = rs.getString("name");
    this.uuid = rs.getString("uuid");
    this.chunkX = rs.getInt("chunkX");
    this.chunkZ = rs.getInt("chunkZ");
    this.blockX = rs.getInt("blockX");
    this.blockY = rs.getInt("blockY");
    this.blockZ = rs.getInt("blockZ");
    return this;
};

As for my initial issue using reflection I dont know why it was failing.