Came across this bit of sample code provided by a lecturer, and noticed something a bit odd: The data in the database must be emptied before executing a new insert query (I presume to avoid redundant data in the database).
Is there any way to avoid this roundabout way of inserting data to the database via hashmaps?
public void saveCustomers() throws ClassNotFoundException, SQLException {
Statement statement = connection.createStatement();
String query = "delete from customer";
statement.executeUpdate(query);
Iterator customers = HRMS.Customers.entrySet().iterator();
while (customers.hasNext()) {
Customer customer;
Map.Entry customerMap = (Map.Entry) customers.next();
customer = (Customer) customerMap.getValue();
query = "insert into customer (memberId, name, nicn, gender, contact) "
+ "values ('" + customer.getMemberId() + "', '" + customer.getName() + "',"
+ " '" + customer.getId() + "', '" + customer.getGender().toString() + "', '" + customer.getContact() + "');";
statement.executeUpdate(query);
}
}
Edit: Will clearing the HashMap
and inserting the data into the database simultaneously (presumably with different threads) be an improvement?