/** * @param index * @throws SQLException */ public void remove(int index) throws SQLException { Connection connection = MySQLDatabase.getConnection(); PreparedStatement preparedStatement = connection.prepareStatement(DB_DELETE_REQUEST); preparedStatement.setInt(1, index); preparedStatement.executeUpdate(); MySQLDatabase.releaseConnection(connection); }
/** * @return amount of requests * @throws SQLException */ public Integer getAmountOfRequests() throws SQLException { Connection c = MySQLDatabase.getConnection(); PreparedStatement s = c.prepareStatement(DB_AMOUNT_REQUESTS); ResultSet rs = s.executeQuery(); int amount = 0; if (rs.next()) { amount = rs.getInt(ColumnNames.AMOUNT); } MySQLDatabase.releaseConnection(c); return amount; }
/** * @param index * @return get different between dates in database * @throws SQLException */ public Integer getDateDiff(int index) throws SQLException { Connection c = MySQLDatabase.getConnection(); PreparedStatement s = c.prepareStatement(DB_GET_DATE_DIFF); s.setInt(1, index); ResultSet rs = s.executeQuery(); if (rs.next()) { return rs.getInt(ColumnNames.DIFF); } MySQLDatabase.releaseConnection(c); return null; }
/** * @return list of requests * @throws SQLException */ public List<RequestRoom> getAll() throws SQLException { Connection c = MySQLDatabase.getConnection(); PreparedStatement s = c.prepareStatement(DB_GET_ALL_REQUESTS); ResultSet rs = s.executeQuery(); List<RequestRoom> result = new ArrayList<>(); while (rs.next()) { result.add(getRequestRoom(rs)); } MySQLDatabase.releaseConnection(c); return result; }
/** * @param from * @param amount * @return list of requests * @throws SQLException */ public List<RequestRoom> getRequests(int from, int amount) throws SQLException { Connection c = MySQLDatabase.getConnection(); String str = DB_GET_ALL_REQUESTS + " limit " + from + ", " + amount; PreparedStatement s = c.prepareStatement(str); ResultSet rs = s.executeQuery(); List<RequestRoom> result = new ArrayList<>(); while (rs.next()) { result.add(getRequestRoom(rs)); } MySQLDatabase.releaseConnection(c); return result; }
/** * @param request * @throws SQLException */ public void add(RequestRoom request) throws SQLException { Connection connection = MySQLDatabase.getConnection(); String arrival = "cast(N'" + request.getArrivalDate() + "' as DateTime)"; String departure = "cast(N'" + request.getDepartureDate() + "' as DateTime)"; String url = String.format(DB_INSERT_REQUEST, arrival, departure); PreparedStatement preparedStatement = connection.prepareStatement(url); preparedStatement.setInt(1, request.getId()); preparedStatement.setInt(2, request.getAmountOfBed()); preparedStatement.setString(3, request.getClassOfRoom()); preparedStatement.setInt(4, request.getClient().getUser().getId()); preparedStatement.executeUpdate(); MySQLDatabase.releaseConnection(connection); }
/** * @param id * @return object RequestRoom * @throws SQLException */ public RequestRoom get(Integer id) throws SQLException { Connection c = MySQLDatabase.getConnection(); PreparedStatement s = c.prepareStatement(DB_GET_REQUEST); s.setInt(1, id); ResultSet rs = s.executeQuery(); RequestRoom requestRoom = new RequestRoom(); if (rs.next()) { requestRoom.setId(id); requestRoom.setClassOfRoom(rs.getString(ColumnNames.CLASS)); requestRoom.setAmountOfBed(rs.getInt(ColumnNames.AMOUNT_OF_BED)); requestRoom.setDepartureDate(rs.getDate(ColumnNames.DEPARTURE_DATE)); requestRoom.setArrivalDate(rs.getDate(ColumnNames.ARRIVAL_DATE)); Client client = new Client(); User user = new User(); user.setId(rs.getInt(ColumnNames.USER_ID)); client.setUser(user); requestRoom.setClient(client); } MySQLDatabase.releaseConnection(c); return requestRoom; }