unable to save data from CSV file to DB in Spring Batch
<p>I'm unable to save data into DB from <code>CSV</code> file in <code>Spring Batch</code>. I've taken a reference from link: <strong><a href="http://www.javabeat.net/spring-batch-example/?et_monarch_popup=true" rel="nofollow noreferrer">http://www.javabeat.net/spring-batch-example/?et_monarch_popup=true</a></strong>, when executing the <code>Main</code> method, I see following tables gets created, but it don't create <code>Employee</code> table. <strong>Why? Please guide, what's wrong happening here?</strong></p>

<p>This doesn't have employee table, also if I create it, it doesn't save data for employee table.
<a href="https://i.stack.imgur.com/j5mpe.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/j5mpe.png" alt="enter image description here"></a></p>

<p>Employee.java</p>

<pre><code>@XmlRootElement(name="employee")
public class Employee implements Serializable {
    private static final long serialVersionUID = 1L;

    private String empId;
    private String city;
    private String country;

    @XmlElement(name="EMP_ID")
    public String getEmpId() {
        return empId;
    }
    public void setEmpId(String empId) {
        this.empId = empId;
    }

    @XmlElement(name="CITY")
    public String getCity() {
        return city;
    }
    public void setCity(String city) {
        this.city = city;
    }

    @XmlElement(name="COUNTRY")
    public String getCountry() {
        return country;
    }
    public void setCountry(String country) {
        this.country = country;
    }
}
</code></pre>

<p>EmployeeDataMapper.java</p>

<pre><code>public class EmployeeDataMapper implements FieldSetMapper&lt;Employee&gt;{

    @Override
    public Employee mapFieldSet(FieldSet fieldSet) throws BindException {
        Employee employee = new Employee();
        employee.setEmpId(fieldSet.readString(0));
        employee.setCity(fieldSet.readString(1));
        employee.setCountry(fieldSet.readString(2));
        return employee;
    }
}
</code></pre>

<p>OrderItemWriter.java</p>

<pre><code>public class OrderItemWriter implements ItemWriter&lt;Employee&gt; {
    private static final String INSERT_EMPLOYEE = "insert into employee(empid,city,country) values(?,?,?)";
    private JdbcTemplate jdbcTemplate;

    public void write(List&lt;? extends Employee&gt; employees) throws Exception {
        for (Employee order : employees) {
            jdbcTemplate.update(INSERT_EMPLOYEE, order.getCity(),order.getCountry(), order.getCountry());
        }
    }

    public OrderItemWriter(DataSource ds) {
        this.jdbcTemplate = new JdbcTemplate(ds);
    }
}
</code></pre>

<p>Main.java</p>

<pre><code>public class Main {
    public static void main(String[] args) {
        final String[] config = { "job-config.xml" };

        ApplicationContext context = new ClassPathXmlApplicationContext(config);

        JobLauncher jobLauncher = (JobLauncher) context.getBean("jobLauncher");
        Job job = (Job) context.getBean("employeeJob");

        try {
            JobExecution execution = jobLauncher.run(job, new JobParameters());
            System.out.println("Exit Status : " + execution.getStatus());
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("~~~~~ Finished Execution of Batch Job ~~~~~~");
    }
}
</code></pre>

<p>job-context.xml</p>

<pre><code>&lt;beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd"&gt;

    &lt;!-- stored job-meta in memory
    &lt;bean id="jobRepository" class="org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean"&gt;
        &lt;property name="transactionManager" ref="transactionManager" /&gt; &lt;/bean&gt;--&gt;

    &lt;!-- stored job-meta in database --&gt;
     &lt;bean id="jobRepository" class="org.springframework.batch.core.repository.support.JobRepositoryFactoryBean"&gt;
        &lt;property name="dataSource" ref="dataSource" /&gt;
        &lt;property name="transactionManager" ref="transactionManager" /&gt;
        &lt;property name="databaseType" value="mysql" /&gt;
    &lt;/bean&gt;

    &lt;bean id="transactionManager"
        class="org.springframework.batch.support.transaction.ResourcelessTransactionManager" /&gt;

    &lt;bean id="jobLauncher" class="org.springframework.batch.core.launch.support.SimpleJobLauncher"&gt;
        &lt;property name="jobRepository" ref="jobRepository" /&gt;
    &lt;/bean&gt;
&lt;/beans&gt;
</code></pre>

<p>job-config.xml</p>

<pre><code>&lt;beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:batch="http://www.springframework.org/schema/batch"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/batch
http://www.springframework.org/schema/batch/spring-batch-3.0.xsd"&gt;

    &lt;import resource="datasource-config.xml" /&gt;
    &lt;import resource="job-context.xml" /&gt;

    &lt;!-- JOB --&gt;
    &lt;job id="employeeJob" xmlns="http://www.springframework.org/schema/batch"&gt;
        &lt;step id="employeeprocessor"&gt;
            &lt;tasklet&gt;
                &lt;chunk reader="reader" writer="writer" commit-interval="3" skip-limit="2"&gt;
                    &lt;skippable-exception-classes&gt;
                        &lt;include class="org.springframework.batch.item.file.FlatFileParseException" /&gt;
                    &lt;/skippable-exception-classes&gt;
                &lt;/chunk&gt;
            &lt;/tasklet&gt;
        &lt;/step&gt;
    &lt;/job&gt;


    &lt;!-- ItemReader --&gt;
    &lt;bean id="reader" class="org.springframework.batch.item.file.FlatFileItemReader" scope="step"&gt;
        &lt;property name="resource" value="classpath:input/employees.csv" /&gt;

        &lt;property name="linesToSkip" value="1" /&gt;

        &lt;property name="lineMapper"&gt;
            &lt;bean class="org.springframework.batch.item.file.mapping.DefaultLineMapper"&gt;
                &lt;property name="lineTokenizer"&gt;
                    &lt;bean class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer"&gt;
                        &lt;property name="names" value="EMP_ID,CITY,COUNTRY" /&gt;
                        &lt;property name="delimiter" value="," /&gt;
                    &lt;/bean&gt;
                &lt;/property&gt;
                &lt;property name="fieldSetMapper"&gt;
                    &lt;bean class="javabeat.net.EmployeeDataMapper" /&gt;
                &lt;/property&gt;
            &lt;/bean&gt;
        &lt;/property&gt;
    &lt;/bean&gt;

    &lt;bean id="writer" class="javabeat.net.OrderItemWriter"&gt;
        &lt;constructor-arg ref="dataSource" /&gt;
    &lt;/bean&gt;
&lt;/beans&gt;
</code></pre>

<p>datasource-config.xml</p>

<pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:jdbc="http://www.springframework.org/schema/jdbc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"&gt;


    &lt;context:property-placeholder location="classpath:database.properties" /&gt;

        &lt;!-- connect to database --&gt;
    &lt;bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"&gt;
        &lt;property name="driverClassName" value="${jdbc.mysql.driverClassName}" /&gt;
        &lt;property name="url" value="${jdbc.mysql.url}" /&gt;
        &lt;property name="username" value="${jdbc.mysql.username}" /&gt;
        &lt;property name="password" value="${jdbc.mysql.password}" /&gt;
    &lt;/bean&gt;

    &lt;bean id="transactionManager"
        class="org.springframework.batch.support.transaction.ResourcelessTransactionManager" /&gt;

    &lt;!-- create job-meta tables automatically In production you don't need to
        create this every time. Just create once in production. --&gt;
    &lt;jdbc:initialize-database data-source="dataSource"&gt;
        &lt;jdbc:script location="org/springframework/batch/core/schema-drop-mysql.sql" /&gt;
        &lt;jdbc:script location="org/springframework/batch/core/schema-mysql.sql" /&gt;
    &lt;/jdbc:initialize-database&gt;
 &lt;/beans&gt;
</code></pre>

<p>employees.csv</p>

<pre><code>1,Pune,India
</code></pre>

<p><strong>Edit-1:</strong>
When I debug the code, I see debug coming on below code</p>

<pre><code>public OrderItemWriter(DataSource ds) {
        this.jdbcTemplate = new JdbcTemplate(ds);
    }
</code></pre>

<p>But <code>write()</code> method not executing. Why ?</p>

<p>DB table I created for reference:</p>

<pre><code>CREATE TABLE `batch`.`employee` (
  `empId` VARCHAR(40) NOT NULL,
  `city` VARCHAR(45) NULL,
  `country` VARCHAR(45) NULL,
  PRIMARY KEY (`empId`)
 );
</code></pre>

<p>database.properties:</p>

<pre><code>jdbc.mysql.driverClassName=com.mysql.jdbc.Driver
jdbc.mysql.url=jdbc:mysql://localhost:3306/batch
jdbc.mysql.username=root
jdbc.mysql.password=password
</code></pre>

<p>pom.xml</p>

<pre><code>&lt;properties&gt;
        &lt;project.build.sourceEncoding&gt;UTF-8&lt;/project.build.sourceEncoding&gt;
        &lt;java.version&gt;1.8&lt;/java.version&gt;
        &lt;springframework.version&gt;4.2.4.RELEASE&lt;/springframework.version&gt;
        &lt;springbatch.version&gt;3.0.6.RELEASE&lt;/springbatch.version&gt;
        &lt;mysql.version&gt;5.1.31&lt;/mysql.version&gt;
        &lt;joda-time.version&gt;2.8&lt;/joda-time.version&gt;
    &lt;/properties&gt;


    &lt;dependencies&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;org.springframework&lt;/groupId&gt;
            &lt;artifactId&gt;spring-core&lt;/artifactId&gt;
            &lt;version&gt;${springframework.version}&lt;/version&gt;
        &lt;/dependency&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;org.springframework&lt;/groupId&gt;
            &lt;artifactId&gt;spring-jdbc&lt;/artifactId&gt;
            &lt;version&gt;${springframework.version}&lt;/version&gt;
        &lt;/dependency&gt;

        &lt;!-- Spring XML to/back object --&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;org.springframework&lt;/groupId&gt;
            &lt;artifactId&gt;spring-oxm&lt;/artifactId&gt;
            &lt;version&gt;${springframework.version}&lt;/version&gt;
        &lt;/dependency&gt;

        &lt;dependency&gt;
            &lt;groupId&gt;org.springframework&lt;/groupId&gt;
            &lt;artifactId&gt;spring-jdbc&lt;/artifactId&gt;
            &lt;version&gt;${springframework.version}&lt;/version&gt;
        &lt;/dependency&gt;

        &lt;!-- Spring Batch --&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;org.springframework.batch&lt;/groupId&gt;
            &lt;artifactId&gt;spring-batch-core&lt;/artifactId&gt;
            &lt;version&gt;${springbatch.version}&lt;/version&gt;
        &lt;/dependency&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;org.springframework.batch&lt;/groupId&gt;
            &lt;artifactId&gt;spring-batch-infrastructure&lt;/artifactId&gt;
            &lt;version&gt;${springbatch.version}&lt;/version&gt;
        &lt;/dependency&gt;

        &lt;dependency&gt;
            &lt;groupId&gt;mysql&lt;/groupId&gt;
            &lt;artifactId&gt;mysql-connector-java&lt;/artifactId&gt;
            &lt;version&gt;${mysql.version}&lt;/version&gt;
        &lt;/dependency&gt;

        &lt;dependency&gt;
            &lt;groupId&gt;joda-time&lt;/groupId&gt;
            &lt;artifactId&gt;joda-time&lt;/artifactId&gt;
            &lt;version&gt;${joda-time.version}&lt;/version&gt;
        &lt;/dependency&gt;

        &lt;dependency&gt;
            &lt;groupId&gt;junit&lt;/groupId&gt;
            &lt;artifactId&gt;junit&lt;/artifactId&gt;
            &lt;version&gt;4.12&lt;/version&gt;
            &lt;scope&gt;test&lt;/scope&gt;
        &lt;/dependency&gt;
    &lt;/dependencies&gt;
</code></pre>