Challenges start with Gradle, Spring, and DB2.

My project idea is to write a simple Spring pure java application using Gradle that will connect to a DB2 database and pull some data and print to the console.

To start, I created a Gradle project using the Eclipse Luna.

My problems:

  • How can I read the database.properties file in src / main / resources which has db.driver, db.url, db.username and db.password?

  • How can I tell Gradle to pick up my db2cc4.jar for the driver? I cannot manage it with Gradle dependencies because it is a patented can.

Here is my build.gradle

apply plugin: 'java'
apply plugin: 'eclipse'

repositories {
    mavenCentral()
 }

jar {
    baseName = 'QueryExecutor'
    version =  '0.1.0'
 }

dependencies {
    compile 'org.springframework:spring-context:4.1.0.RELEASE'
    compile 'org.springframework:spring-jdbc:4.1.0.RELEASE'
    runtime files('lib/db2cc4.jar') 
    testCompile 'junit:junit:4.+'
  }

task wrapper(type: Wrapper) {
    gradleVersion = '2.1'
}

Here is my ApplicationConfig.java

import javax.annotation.Resource;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;

import org.shrinathk.queryexecutor.persistence.QueryExecutorDAO;

@Configuration
@Import({DatabaseConfig.class})
@PropertySource("classpath:application.properties")
@PropertySource("classpath:database.properties")
public class ApplicationConfig
{
    @Resource
    private Environment env;

    @Bean
    public QueryExecutorDAO queryExecutor()
    {
    return new org.shrinathk.queryexecutor.persistence.QueryExecutorDAO();
    }
}

Here is my DatabaseConfig.java

import javax.annotation.Resource;
import javax.sql.DataSource;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@Configuration
@EnableTransactionManagement
public class DatabaseConfig
{

    private static final String PROPERTY_NAME_DATABASE_DRIVER   = "db.driver";
    private static final String PROPERTY_NAME_DATABASE_URL      = "db.url";
    private static final String PROPERTY_NAME_DATABASE_USERNAME = "db.username";
    private static final String PROPERTY_NAME_DATABASE_PASSWORD = "db.password";

    @Resource
    private Environment env;

    @Bean
    public DataSource dataSource()
    {
    DriverManagerDataSource dataSource = new DriverManagerDataSource();

    dataSource.setDriverClassName(env.getRequiredProperty(PROPERTY_NAME_DATABASE_DRIVER));
    dataSource.setUrl(env.getRequiredProperty(PROPERTY_NAME_DATABASE_URL));
    dataSource.setUsername(env.getRequiredProperty(PROPERTY_NAME_DATABASE_USERNAME));
    dataSource.setPassword(env.getRequiredProperty(PROPERTY_NAME_DATABASE_PASSWORD));

    return dataSource;
    }
}

Database.properties file

db.driver=com.ibm.db2.jcc.DB2Driver
db.url=jdbc:db2://localhost:50000/SAMPLE
db.username=db2admin
db.password=db2admin

Here is my project structure:

Project structure

Thank!

+4
1
  • (, src/main/resources) , maven. , .

  • db2jcc.ar, flatDir dependencies:

    compile fileTree(dir: 'lib', include: '*.jar')

+3

All Articles