Home  >  Forum  >  Spring
Post New Query

Spring batch csv to db



Asked on May 30, 2019
Hi i am new to spring
I have got assignment to do which include reading a csv file from ftp location and insert that csv elements to mysql database using spring batch 
Note: only using spring batch not boot , kindy guide me



Replied on June 01, 2019
Find the link that can help you.


Above link read csv file from classpath. We can read file from ftp using URL.

URL url = new URL ("ftp://username:[email protected]");
URLConnection urlc = url.openConnection();
InputStream is = urlc.getInputStream();

We can write item in database using JdbcBatchItemWriter.

    @Bean
    public ItemWriter<Marksheet> writer(DataSource dataSource) {
        JdbcBatchItemWriter<Marksheet> writer = new JdbcBatchItemWriter<Marksheet>();
        writer.setItemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider<Marksheet>());
        writer.setSql("INSERT INTO marksheet (studentId,totalMark) VALUES (:stdId,:totalSubMark)");
        writer.setDataSource(dataSource);
        return writer;
    }



Write Answer







©2019 concretepage.com | Privacy Policy | Contact Us