2016-12-02 20 views
0

私はオブジェクト予約と予約の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; 
} 

答えて

0

RESERVATION_IDの列にAUTO_INCREMENTを使用して(シーケンスを生成するために)、mysqlデータベーステーブルにhereと表示され、AUTO_INCREMENTをmysqlデータベーステーブルに設定することができます。

あなたはAUTO_INCREMENTの設定が完了したら、以下のように、あなたのJDBC問合せの列RESERVATION_IDnewReservation.getReserveID()を渡す必要はありません。

String insRes = "INSERT INTO " + RESERVATIONTABLE 
      + " (GUEST_ID, ROOM_ID, DURATION, STATUS) VALUES (" 
      + newReservation.getGuestID() + ", " 
      + newReservation.getRoomID() + ", " 
      + newReservation.getStayDuration() + ", " 
      + newReservation.getCheck() + ")";  

を私は強く上のハードコーディングを使用しないことをお勧めします(はSQLインジェクション攻撃になる傾向があります)、prepareStatementsetterのように、hereのような方法を使用してください。

+0

テーブルが自動インクリメントに設定されていますが、予約のデフォルトコンストラクターに問題があります。私はreservationID = 0を設定し、自動生成された値を書き換えています。 – Dan

+0

上書きするとどういう意味ですか?それはデータベースにありますか?値は上書きされていますか? – developer

+0

私は問題を解決しましたが、助けてくれてありがとう – Dan

関連する問題