Igor
Igor

Reputation: 866

mybatis-guice mysql failed to load jdbc driver

I'm trying to use mybatis-guice with mysql but I keep catching the next exception:

Error querying database. Cause: java.sql.SQLException: Error setting driver on UnpooledDataSource. Cause: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

Cause: java.sql.SQLException: Error setting driver on UnpooledDataSource. Cause: java.lang.ClassNotFoundException: org.hsqldb.jdbcDriver

Here is the code I try to run:

public class DaoImpl implements Dao {
    @Inject
    private MyMapper mapper;

    public String getSomething() {
        return mapper.getSomething();
    }
}

public interface Dao {
    String getSomething();
}

public interface MyMapper {
    @Select("SELECT DATE_FORMAT(NOW(),'%d/%m/%Y %H:%i') as time")
    String getSomething();
}

Injector injector = Guice.createInjector(
        new MyBatisModule() {
            @Override
            protected void initialize() {
                install(JdbcHelper.MySQL);
                environmentId("development");

                bindDataSourceProviderType(PooledDataSourceProvider.class);
                bindTransactionFactoryType(JdbcTransactionFactory.class);
                addMapperClass(MyMapper.class);

                bindProperties(binder(), getMybatisProperties());
                bind(Dao.class).to(DaoImpl.class);
            }
        }
);

private Properties getMybatisProperties() {
    Properties myBatisProperties = new Properties();
    myBatisProperties.setProperty("JDBC.host", "127.0.0.1");
    myBatisProperties.setProperty("JDBC.port", "3306");
    myBatisProperties.setProperty("JDBC.schema", "my_schema");
    myBatisProperties.setProperty("JDBC.driver", "com.mysql.jdbc.Driver"));
    myBatisProperties.setProperty("JDBC.username", "root");
    myBatisProperties.setProperty("JDBC.password", "");
    myBatisProperties.setProperty("JDBC.autoCommit", "false");
    return myBatisProperties;
}

And than I try to run: injector.getInstance(Dao.class).getSomething()

I tried to remove myBatisProperties.setProperty("JDBC.driver", "com.mysql.jdbc.Driver")); but the result was the same.

Also, after several hours of debugging the code, install(JdbcHelper.MySQL); should add the driver by itself. Is this assumption correct?

The exception is thrown on mapper.getSomething();

Ideas???

Upvotes: 0

Views: 3484

Answers (1)

Mike Hurtado A
Mike Hurtado A

Reputation: 26

You need a driver mysql-connector-java or verify the version of the driver and your put mysql-connector-java-bin.jar in the lib

Upvotes: 1

Related Questions