JDBC Helper
Bind automatically JDBC Connection URL and Driver
For those users (like me!!!) don't have a dictionary to quickly retrieve the JDBC url pattern and the driver class name for a specific DBMS, here it comes the org.mybatis.guice.datasource.helper.JdbcHelper Module.
It is an easy to use Google Guice module that makes easy the JDBC.url and JDBC.driver properties binding needed for the DataSource providers.
The org.mybatis.guice.datasource.helper.JdbcHelper provides a large number of Enumeration values that cover the most popular DBMS (both commercial and open source) that are provided/recommended by the actual database vendor, resumed in the table below.
Variable Format Patterns contain variables in the Apache ANT alike style, in the format ${name|defaultValue}, where name is used as label which will looked for in the current Guice Injector, with defaultValue as its default value, replaced in the pattern if name is not found.
NOTE this feature doesn't work with XML Module!!!
Supported DBMS
| DBMS | URL pattern |
|---|---|
JdbcHelper.Cache |
jdbc:Cache://${JDBC.host|localhost}:${JDBC.port|1972}/${JDBC.schema} |
JdbcHelper.Daffodil_DB |
jdbc:daffodilDB://${JDBC.host|localhost}:${JDBC.port|3456}/${JDBC.schema} |
JdbcHelper.DB2 |
jdbc:db2://${JDBC.host|localhost}:${JDBC.port|50000}/${JDBC.schema} |
JdbcHelper.DB2_DataDirect |
jdbc:datadirect:db2://${JDBC.host|localhost}:${JDBC.port|50000}/DatabaseName=${JDBC.schema} |
JdbcHelper.DB2_AS400_JTOpen |
jdbc:as400://${JDBC.host|localhost} |
JdbcHelper.Firebird |
jdbc:firebirdsql:${JDBC.host|localhost}/${JDBC.port|3050}:${JDBC.schema} |
JdbcHelper.FrontBase |
jdbc:FrontBase://${JDBC.host|localhost}/${JDBC.schema} |
JdbcHelper.HP_Neoview |
jdbc:hpt4jdbc://${neoview.system}:${JDBC.port}/: |
JdbcHelper.HSQLDB_Server |
jdbc:hsqldb:hsql://${JDBC.host|localhost}:${JDBC.port|9001}/${JDBC.schema} |
JdbcHelper.HSQLDB_Embedded |
jdbc:hsqldb:${JDBC.schema} |
JdbcHelper.H2_IN_MEMORY_PRIVATE |
jdbc:h2:mem" |
JdbcHelper.H2_IN_MEMORY_NAMED |
jdbc:h2:mem:${JDBC.schema}" |
JdbcHelper.H2_SERVER_TCP |
jdbc:h2:tcp://${JDBC.host|localhost}:${JDBC.port|9001}/${JDBC.schema}" |
JdbcHelper.H2_SERVER_SSL |
jdbc:h2:ssl://${JDBC.host|localhost}:${JDBC.port|9001}/${JDBC.schema}" |
JdbcHelper.H2_FILE |
jdbc:h2:file://${JDBC.schema}" |
JdbcHelper.H2_EMBEDDED |
jdbc:h2:${JDBC.schema}" |
JdbcHelper.Informix |
jdbc:informix-sqli://${JDBC.host|localhost}:${JDBC.port|1533}/${JDBC.schema}: informixserver=${dbservername} |
JdbcHelper.Informix_DataDirect |
jdbc:datadirect:informix://${JDBC.host|localhost}:${JDBC.port|1533}; InformixServer=${informixserver};DatabaseServer=${JDBC.schema} |
JdbcHelper.Derby_Server |
jdbc:derby://${JDBC.host|localhost}:${JDBC.port|1527}/${JDBC.schema} |
JdbcHelper.Derby_Embedded |
jdbc:derby:${JDBC.schema};create=${derby.create|false} |
JdbcHelper.JDataStore |
jdbc:borland:dslocal:${JDBC.schema} |
JdbcHelper.JDBC_ODBC_Bridge |
jdbc:odbc:${ODBC.datasource} |
JdbcHelper.MariaDB |
jdbc:mysql://${JDBC.host|localhost}:${JDBC.port|3306}/${JDBC.schema} |
JdbcHelper.MaxDB |
jdbc:sapdb://${JDBC.host|localhost}:${JDBC.port|7210}/${JDBC.schema} |
JdbcHelper.McKoi |
jdbc:mckoi://${JDBC.host|localhost}:${JDBC.port|9157}/${JDBC.schema} |
JdbcHelper.Mimer |
jdbc:mimer:${mimer.protocol}://${JDBC.host|localhost}:${JDBC.port|1360}/${JDBC.schema} |
JdbcHelper.MySQL |
jdbc:mysql://${JDBC.host|localhost}:${JDBC.port|3306}/${JDBC.schema} |
JdbcHelper.Netezza |
jdbc:netezza://${JDBC.host|localhost}:${JDBC.port|5480}/${JDBC.schema} |
JdbcHelper.Oracle_Thin |
jdbc:oracle:thin:@${JDBC.host|localhost}:${JDBC.port|1521}:${oracle.sid|ORCL} |
JdbcHelper.Oracle_OCI |
jdbc:oracle:oci:@${JDBC.host|localhost}:${JDBC.port|1521}:${oracle.sid|ORCL} |
JdbcHelper.Oracle_DataDirect |
jdbc:datadirect:oracle://${JDBC.host|localhost}:${JDBC.port|1521}; ServiceName=${oracle.servicename|ORCL} |
JdbcHelper.Pervasive |
jdbc:pervasive://${JDBC.host|localhost}:${JDBC.port}/${JDBC.schema} |
JdbcHelper.Pointbase_Embedded |
jdbc:pointbase:embedded:${JDBC.schema},database.home=${pointbase.home} ,create=${pointbase.create|false} |
JdbcHelper.Pointbase_Server |
jdbc:pointbase:server://${JDBC.host|localhost}:${JDBC.port|9092}/${JDBC.schema} ,database.home=${pointbase.home},create=${pointbase.create|false} |
JdbcHelper.PostgreSQL |
jdbc:postgresql://${JDBC.host|localhost}:${JDBC.port|5432}/${JDBC.schema} |
JdbcHelper.Progress |
jdbc:jdbcProgress:T:${JDBC.host|localhost}:${JDBC.port|2055}:${JDBC.schema} |
JdbcHelper.SQL_Server_DataDirect |
jdbc:datadirect:sqlserver://${JDBC.host|localhost}:${JDBC.port|1433}; DatabaseName=${JDBC.schema|Northwind} |
JdbcHelper.SQL_Server_jTDS |
jdbc:jtds:sqlserver://${JDBC.host|localhost}:${JDBC.port|1433}; DatabaseName=${JDBC.schema|Northwind};domain=${sqlserver.domain} |
JdbcHelper.SQL_Server_MS_Driver |
jdbc:microsoft:sqlserver://${JDBC.host|localhost}:${JDBC.port|1433}; DatabaseName=${JDBC.schema|Northwind} |
JdbcHelper.SQL_Server_2005_MS_Driver |
jdbc:sqlserver://${JDBC.host|localhost}:${JDBC.port|1433};DatabaseName=${JDBC.schema|Northwind} |
JdbcHelper.Sybase_ASE_jTDS |
jdbc:jtds:sybase://${JDBC.host|localhost}:${JDBC.port|5000};DatabaseName=${JDBC.schema} |
JdbcHelper.Sybase_ASE_JConnect |
jdbc:sybase:Tds:${JDBC.host|localhost}:${JDBC.port|5000}/${JDBC.schema} |
JdbcHelper.Sybase_SQL_Anywhere_JConnect |
jdbc:sybase:Tds:${JDBC.host|localhost}:${JDBC.port|2638}/${JDBC.schema} |
JdbcHelper.Sybase_DataDirect |
jdbc:datadirect:sybase://${JDBC.host|localhost}:${JDBC.port|2048};ServiceName=${JDBC.schema} |
Usage Example
Using the helper is simpler than explain how it works, let's have a look at the code below that configures the PooledDataSourceProvider for MySQL:
final Properties myBatisProperties = new Properties();
myBatisProperties.setProperty("mybatis.environment.id", "test");
// configure the database host
myBatisProperties.setProperty("JDBC.host", "contacts.acme.db");
// configure the database port
myBatisProperties.setProperty("JDBC.port", "6969");
// configure the database schema
myBatisProperties.setProperty("JDBC.schema", "contacts_test");
myBatisProperties.setProperty("JDBC.username", "fooTest");
myBatisProperties.setProperty("JDBC.password", "barPWD");
myBatisProperties.setProperty("JDBC.autoCommit", "false");
// binds the JDBC connection URL and the Driver class name for MySQL
Injector injector = Guice.createInjector(JdbcHelper.MySQL,
new MyBatisModule() {
public void configure(Binder binder) {
setDataSourceProviderType(PooledDataSourceProvider.class);
addSimpleAliases(Contact.class);
addMapperClasses(ContactMapper.class);
}
},
new Module() {
public void configure(Binder binder) {
/* binds the properties configuration; JDBC.host,
* JDBC.port and JDBC.schema will be used to be
* replaced in the URL Pattern
*/
Names.bindProperties(binder, myBatisProperties);
}
});
MyBatis