I am working on a Java program and one of the main requirements involves uploading a csv file which will in turn update the database.
Everything seems to be working fine when I upload a file expect for every row the first columns value is read in as null, while the the rest of the columns are populated correctly.
It is my first time using spring, hibernate and maven on a project so I'm learning as I go. It's probably something simple but I've tried everything I can think of for this and would very much appreciate some help.
public static List createMembers(CSVReader csvReader) throws Exception{
ColumnPositionMappingStrategy<Member> strategy = new ColumnPositionMappingStrategy <Member>();
strategy.setType(Member.class);
String[] columns = new String[] {"forename","surname","dob","player", "address1","address2","address3","address4","town","county","postcode","mobile","home","work","email","otherInformation"};
strategy.setColumnMapping(columns);
CsvToBean csvToBean = new CsvToBean();
//Set column mapping strategy
List list = csvToBean.parse(strategy, csvReader);
return list;
}
--
public void importCSV() throws Exception {
CSVReader csvReader = new CSVReader(new FileReader(*filename*));
@SuppressWarnings("rawtypes")
List list = XXCsvToObject.createMembers(csvReader);
//logger.info(csvReader);
Session session = this.sessionFactory.getCurrentSession();
for (Object object : list) {
Member member = (Member) object;
session.persist(member);
logger.info(member.toString());
}
}
--
@Table(name="MEMBER")
public class Member{
@Id
@Column(name="id")
@GeneratedValue(strategy=GenerationType.IDENTITY)
private int id;
private String forename;
private String surname;
private String dob;
private String player;
private String address1;
private String address2;
private String address3;
private String address4;
private String town;
private String county;
private String postcode;
private String mobile;
private String home;
private String work;
private String email;
private String otherInformation;
--
Peter Jones 23/02/2010 Y 15 Main street Peckham Peckham London SE15 3AA
331518315 [email protected]
Mary Jones 04/06/2002 Y 15 Main street Peckham Peckham London SE15 3AA
576846958 [email protected]
John Jones 10/09/1979 N 15 Main street Peckham Peckham London SE15 3AA
576864694 [email protected]