variable counter is not incrementing with adding new booking objects
// Entity class for booking
public class Booking {
private int distance;
private static int bookingId = 1000;
private int cabId;
private int customerId;
private int billingAmount;
public Booking() {
bookingId++;
System.out.println(bookingId);
}
}
// Service class for booking, I'm setting up all parameters here.
public class BookCab {
public Booking bookCab(int distance, int customerId){
Booking book = new Booking();
CabDao cabDao = new CabDaoImpl();
book.setDistance(distance);
book.setCustomerId(customerId);
book.setCabId(cabDao.getCabId()); //retrieving cab data from database
if(distance>10) {book.setBillingAmount(305 + (distance-10)*25);}
else if (distance <10 & distance>5) {
book.setBillingAmount(180 + (distance-5)*25);
}
else if (distance <5 & distance>1) {
book.setBillingAmount(100 + (distance-1)*20);
}
else book.setBillingAmount(100);
return book;
}
}
// This is DAO Implementation for accessing the Database.
public class BookingDaoImpl implements BookingDao {
Connection connection = null;
PreparedStatement ptmt = null;
ResultSet resultSet = null;
public BookingDaoImpl() {};
private Connection getConnection() throws SQLException {
Connection conn;
conn = DbUtil.getInstance().getConnection();
return conn;
}
public void addBooking(Booking booking) {
try {
String queryString = "INSERT INTO Booking(bookingId, customerId, cabId, distance, chargingAmount) VALUES(?,?,?,?,?)";
connection = getConnection();
ptmt = connection.prepareStatement(queryString);
ptmt.setInt(1, booking.getBookingId());
ptmt.setInt(2, booking.getCustomerId());
ptmt.setInt(3, booking.getCabId());
ptmt.setInt(4, booking.getDistance());
ptmt.setInt(5, booking.getBillingAmount());
ptmt.executeUpdate();
System.out.println("Data Added Successfully");
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (ptmt != null)
ptmt.close();
if (connection != null)
connection.close();
} catch (SQLException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
// Driver class
public class MainView {
public static void main(String[] args) {
BookCab bookCab = new BookCab();
BookingDao bookingDao = new BookingDaoImpl();
bookingDao.addBooking(bookCab.bookCab(16, 691744));
}
}
/* Output
1001
Data Added Successfully
1001
Data Added Successfully
1001
Data Added Successfully
*/
The counter is not working, booking id is not incremented.
I've tried printing counter right after incrementing, but it's the same there as well.