I am trying to insert into a table without duplicates. I am using SQL derbyclient
in Java. The code is not working (error with 'where not exists'
). Any idea?
Connection connection = DriverManager.getConnection("jdbc:derby://localhost:1527/PetroleumDataStorageSystemDB;create=true");
PreparedStatement statement;
int i = 1;
int progress = 0;
for (Refuel refuelList1 : refuelList) {
progress = (i / refuelList.size()) * 100;
String sql = "INSERT INTO refuel (id_tank, time, fuel_volume, "
+ "refueling_speed) VALUES (?,?,?,?) "
+ "WHERE NOT EXISTS (SELECT * FROM refuel WHERE "
+ "id_tank = ? AND time = ? AND fuel_volume = ? AND "
+ "refueling_speed = ?)";
statement = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
statement.setInt(1, refuelList1.getTankID());
statement.setString(2, refuelList1.getTime());
statement.setDouble(3, refuelList1.getFuelVolume());
statement.setDouble(4, refuelList1.getRefuelingSpeed());
statement.execute();
i++;
}