私はオブジェクト予約と予約のarrayListを作成し、JDBCを使用して予約のデータベースを操作しています。私はテーブルに予約を挿入することができますが、私は予約オブジェクトのすべての属性の値をmainに入力する必要があります。ただし、データベースは予約IDを自動生成するように設定されています。私の質問は、データベースに挿入するときに、私がテーブルに挿入するときに代わりに自動生成されたIDが入力されるメインのreservationIDを入力するのではなく、代わりに入力できるようにする方法です。これは初めてデータベースで作業するので、少し新しくなりました。JDBC、ユーザー定義のオブジェクトとユーザー定義オブジェクトのarraylist
予約:予約の
/** The Reserve ID. */
int ReserveID;
/** The Room ID. */
int RoomID;
/** The Guest ID. */
int GuestID;
/** The Stay duration. */
int StayDuration;
/** The check. */
boolean Check;
public reservation()
{
ReserveID = 0;
GuestID = 0;
RoomID = 0;
StayDuration = 0;
Check = false;
}
/**
* Instantiates a new reservation.
*
* @param reserveID the reserve ID
* @param roomID the room ID
* @param guestID the guest ID
* @param stayDuration the stay duration
* @param check the check
*/
public reservation(int reserveID, int guestID, int roomID, int stayDuration, boolean check) {
ReserveID = reserveID;
GuestID = guestID;
RoomID = roomID;
StayDuration = stayDuration;
Check = check;
}
のArrayList:
/** The res list. */
ArrayList<reservation> resList;
/**
* Instantiates a new reservation collection.
*/
public reservationCollection()
{
resList = new ArrayList<reservation>();
}
/**
* Adds the reservation.
*
* @param r the r
* @return true, if successful
*/
public boolean addReservation(reservation r) throws SQLException {
resList.add(r);
reservationJDBC.insertReservation(r);
return true;
}
JDBCクラスの挿入方法:メイン
public static boolean insertReservation(reservation newReservation) throws SQLException {
Connection dbConnection = null;
Statement statement = null;
String insRes = "INSERT INTO " + RESERVATIONTABLE
+ " (RESERVATION_ID, GUEST_ID, ROOM_ID, DURATION, STATUS) VALUES ("
+ newReservation.getReserveID() + ", "
+ newReservation.getGuestID() + ", "
+ newReservation.getRoomID() + ", "
+ newReservation.getStayDuration() + ", "
+ newReservation.getCheck() + ")";
try {
dbConnection = getDBConnection();
statement = dbConnection.createStatement();
statement.executeUpdate(insRes);
System.out.println("Record is inserted into DBUSER table!");
} catch (SQLException e) {
System.out.println(e.getMessage());
} finally {
if (statement != null) {
statement.close();
}
if (dbConnection != null) {
dbConnection.close();
}
}
return false;
}
:
System.out.println("Hotel Management System - Test 1");
reservationCollection myCollection = new reservationCollection();
boolean on = true;
while(on){
reservation r = new reservation(45, 34, 56, 78, false);
if(myCollection.addReservation(r)){
System.out.println("Success! The reservation has been inserted to database");
} else {
System.out.println("Failed! There has been a problem with inserting reservation to the database");
}
on = false;
}
テーブルが自動インクリメントに設定されていますが、予約のデフォルトコンストラクターに問題があります。私はreservationID = 0を設定し、自動生成された値を書き換えています。 – Dan
上書きするとどういう意味ですか?それはデータベースにありますか?値は上書きされていますか? – developer
私は問題を解決しましたが、助けてくれてありがとう – Dan