JDBC,用户定义的对象和用户定义对象的arraylist

时间:2016-12-02 18:48:36

标签: java mysql jdbc arraylist

我创建了一个对象预留和预留的arrayList,并使用JDBC来处理预留数据库。我能够在表中插入一个预留,但我必须在main中输入预留对象的所有属性的值。但是,数据库设置为自动生成reservationID。我的问题是我如何才能这样做,以便当我插入数据库时​​,而不是我输入main中的reservationID,当我插入表格时,输入自动生成的ID代替。这是我第一次使用数据库,所以对此有点新鲜。

预订:

/** 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 个答案:

没有答案