I am forced to build the JAR file with JDK 6 because it will be used on a company laptop and the laptop owner cannot update their Java version without the laptop going through the IT people.
So, how to work around the try-with-resources error for this method:
public static String importFile(String filepath){
String insertQuery = "INSERT INTO SALESMAN VALUES (?,?)";
String status;
try (CSVReader reader = new CSVReader(new FileReader(filepath), ','); //error here
Connection connection = DBConnection.getConnection();){
Statement stmt = connection.createStatement();
PreparedStatement pstmt = connection.prepareStatement(insertQuery);
String[] rowData = null;
int i = 0;
while((rowData = reader.readNext()) != null){
for (String data : rowData){
pstmt.setString((i % 2) + 1, data);
if (++i % 2 == 0)
pstmt.addBatch();
if (i % 20 == 0)
pstmt.executeBatch();
}
}
status = "Successfully uploaded";
} catch (Exception ex) {
ex.printStackTrace();
}
return status;
}
The try-with-resource syntax was only introduced in Java 7. If you're forced to use Java 6, you'd have to resort to a good old fashioned finally
clause:
CSVReader reader = null;
try {
reader = new CSVReader(new FileReader(filepath), ',');
// Code from the original try block, removed for brevity's sake
} catch (Exception ex) {
ex.printStackTrace(); // Or some useful error handling
} finally { // closing the reader in the finally block
if (reader != null) {
reader.close();
}
}