//预订实体类
公共课预订{
private int distance;
private static int bookingId = 1000;
private int cabId;
private int customerId;
private int billingAmount;
public Booking() {
bookingId++;
System.out.println(bookingId);
}
}
//预订服务类,我在这里设置所有参数。
公共类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;
}
}
//这是访问数据库的DAO实现。
公共类BookingDaoImpl实现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();
}
}
}
}
//驱动程序类
公共类MainView {
public static void main(String[] args) {
BookCab bookCab = new BookCab();
BookingDao bookingDao = new BookingDaoImpl();
bookingDao.addBooking(bookCab.bookCab(16, 691744));
}
}
/ *输出
1001
Data Added Successfully
1001
Data Added Successfully
1001
Data Added Successfully
* /
Counter is not working
booking id is not increamenting
我已经尝试在增加后立即打印计数器,但它也一样。
答案 0 :(得分:0)
您只需在Booking
中创建一个main
个实例(或者,确切地说,您创建一个BookCab
个实例并调用bookCab.bookCab(16, 691744)
,这会创建一个Booking
{ {1}}实例),这意味着它应该打印1001。
如果多次运行该应用程序(根据您的输出,我假设您这样做),Booking
类将在每次执行时加载并初始化,因此它会将计数器重置为1000。
如果希望该static
变量的值贯穿应用程序的执行,则必须将其保留在某个文件或数据库表中,并在初始化Booking
类时加载它。
P.S。,您可能希望将当前bookingId
值存储在某个实例变量中,以便每个Booking
实例都与唯一的bookingId
值相关联。
答案 1 :(得分:0)
您的主要方法仅创建一个预订,每次运行应用程序时都会生成Id 1001。
在main方法中创建一个循环以进行多个预订,并检查它是否正在增加。
所以改变主要来自
public static void main(String[] args) {
BookCab bookCab = new BookCab();
BookingDao bookingDao = new BookingDaoImpl();
bookingDao.addBooking(bookCab.bookCab(16, 691744));
}
到
public static void main(String[] args) {
for (int i = 0; i < 3; ++i) {
BookCab bookCab = new BookCab();
BookingDao bookingDao = new BookingDaoImpl();
bookingDao.addBooking(bookCab.bookCab(16, 691744));
}
}
答案 2 :(得分:0)
静态变量增量无法帮助您解决整体实现问题,它只是在为Booking类创建多个对象时按预期工作,但是如果应用程序重新启动,请抓住这里的内容?
计数器设置回起点1000.
@Eran已经在评论中指出了这个问题。