In my case I have class and subclass. Problem is when I search my function increment id automatically. When I first time click search it upload 5 ids, when I add one and go back to search it upload 5 ids I have + (5 old + 1 new). Then if I want to add new one it gives him id 5+6 + 1 new = 13 id.
Class auto long has id and name:
public static AtomicInteger idSequence = new AtomicInteger();
public Car(String name) {
this.id = (long)idSequence.incrementAndGet();
this.name= name;
}
Subclass:
public OldCars(String name){
super(name);
}
Function to update from H2:
public static List<Car> uploadAllCars() throws SQLException, IOException {
Connection connection= connectToDatabase();
List<Car> listOfCars= new ArrayList<>();
Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM CARS");
while(rs.next()){
Long id = rs.getLong("id");
String name= rs.getString("name");
Car car= new Car(name);
car.setId(id);
listOfCars.add(car);
}
closeConnectionToDataBase(connection);
return listOfCars;}
Problem is how to stop auto increment and call function without sum of ids. I'm calling uploadAllCars function to search every time specific car but it auto increment instantly.
Edit: I want from user to input name (not id)
define another constructor for getting cars from database like this:
and use that instead of
new Car(name)
, sonew Car(name, id)