I'm trying to upload a CSV file into a database that contains student results. I want to update a field for student records that already exist in the database and insert new records for those that do not. The CSV file includes records for both new and existing students.
I am using two fields - idnumber
and courseCode
to uniquely identify student records in the database and the CSV file.
Here is what I have tried so far... Please can anybody help me?
try{
String updateSql = "update ResultUpload set idnumber = ?,ca = ?, exams = ?, total = ?, AAA=?,BPLUS=?,BBB=?,CPLUS=?,CCC=?,DDD=?,EEE=?,FFF=?,gpp=?,ugp=?,remarks=?,unit=?,level=?,courseCode=?,courseName=?,semester=? where idnumber=? and courseCode=?";
String insertSql = "Insert into ResultUpload (idnumber,ca,exams,total,AAA,BPLUS,BBB,CPLUS,CCC,DDD,EEE,FFF,gpp,ugp,remarks,unit,level,courseCode,courseName,semester) values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
String sql = "select idnumber, courseCode from ResultUpload where idnumber = ? and courseCode = ?"; // no need to execute this statement inside a loop
BufferedReader br = new BufferedReader(new FileReader(filename));
Connection conn = null;
PreparedStatement insertStatement = conn.prepareStatement(insertSql);
PreparedStatement updateStatement = conn.prepareStatement(updateSql);
PreparedStatement pst = conn.prepareStatement(sql);
String line;
while ((line = br.readLine()) != null) {
String[] value = line.split(","); // check this line that it is not null
pst.setString(1, value[0]);
pst.setString(2, value[17]);
ResultSet rs = pst.executeQuery();
if (rs.next()) { // if there is an entry in the DB make update
updateRecord(updateStatement, value);
update_table();
} else {
insertRecord(insertStatement, value);
update_table();
}
rs.close();
}
br.close(); // close these resources in the finally block
insertStatement.close();
updateStatement.close();
conn.close();
}
catch (SQLException | IOException ex) {
ex.printStackTrace(); // handle exception here
}
// handle exception here
finally{
try{
pst.close();
rs.close();
}
catch(Exception e){}
}
}
}
private static void updateRecord(PreparedStatement preparedStatement,String[] value) throws SQLException {
preparedStatement.setString(1, value[0]);
preparedStatement.setString(2, value[1]);
preparedStatement.setString(3, value[2]);
preparedStatement.setString(4, value[3]);
preparedStatement.setString(5, value[4]);
preparedStatement.setString(6, value[5]);
preparedStatement.setString(7, value[6]);
preparedStatement.setString(8, value[7]);
preparedStatement.setString(9, value[8]);
preparedStatement.setString(10, value[9]);
preparedStatement.setString(11, value[10]);
preparedStatement.setString(12, value[11]);
preparedStatement.setString(13, value[12]);
preparedStatement.setString(14, value[13]);
preparedStatement.setString(15, value[14]);
preparedStatement.setString(16, value[15]);
preparedStatement.setString(17, value[16]);
preparedStatement.setString(18, value[17]);
preparedStatement.setString(19, value[18]);
preparedStatement.setString(20, value[19]);
preparedStatement.executeUpdate();
}
private static void insertRecord(PreparedStatement preparedStatement,String[] value) throws SQLException {
preparedStatement.setString(1, value[0]);
preparedStatement.setString(2, value[1]);
preparedStatement.setString(3, value[2]);
preparedStatement.setString(4, value[3]);
preparedStatement.setString(5, value[4]);
preparedStatement.setString(6, value[5]);
preparedStatement.setString(7, value[6]);
preparedStatement.setString(8, value[7]);
preparedStatement.setString(9, value[8]);
preparedStatement.setString(10, value[9]);
preparedStatement.setString(11, value[10]);
preparedStatement.setString(12, value[11]);
preparedStatement.setString(13, value[12]);
preparedStatement.setString(14, value[13]);
preparedStatement.setString(15, value[14]);
preparedStatement.setString(16, value[15]);
preparedStatement.setString(17, value[16]);
preparedStatement.setString(18, value[17]);
preparedStatement.setString(19, value[18]);
preparedStatement.setString(20, value[19]);
preparedStatement.executeUpdate();
}